Patch from Konstantin Isakov <ikm@pisem.net>:
[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 extern char get_header_tar(archive_handle_t *archive_handle)
24 {
25         file_header_t *file_header = archive_handle->file_header;
26         union {
27                 unsigned char raw[512];
28                 struct {
29                         char name[100]; /*   0-99 */
30                         char mode[8];   /* 100-107 */
31                         char uid[8];    /* 108-115 */
32                         char gid[8];    /* 116-123 */
33                         char size[12];  /* 124-135 */
34                         char mtime[12]; /* 136-147 */
35                         char chksum[8]; /* 148-155 */
36                         char typeflag;  /* 156-156 */
37                         char linkname[100];     /* 157-256 */
38                         char magic[6];  /* 257-262 */
39                         char version[2];        /* 263-264 */
40                         char uname[32]; /* 265-296 */
41                         char gname[32]; /* 297-328 */
42                         char devmajor[8];       /* 329-336 */
43                         char devminor[8];       /* 337-344 */
44                         char prefix[155];       /* 345-499 */
45                         char padding[12];       /* 500-512 */
46                 } formated;
47         } tar;
48         long sum = 0;
49         long i;
50
51         /* Align header */
52         archive_handle->offset += data_align(archive_handle->src_fd, archive_handle->offset, 512);
53
54         if (xread_all_eof(archive_handle->src_fd, tar.raw, 512) == 0) {
55                 /* End of file */
56                 return(EXIT_FAILURE);
57         }
58         archive_handle->offset += 512;
59
60         /* If there is no filename its an empty header */
61         if (tar.formated.name[0] == 0) {
62                 return(EXIT_SUCCESS);
63         }
64
65         /* Check header has valid magic, "ustar" is for the proper tar
66          * 0's are for the old tar format
67          */
68         if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
69 #ifdef CONFIG_FEATURE_TAR_OLD_FORMAT
70                 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
71 #endif
72                         error_msg_and_die("Invalid tar magic");
73         }
74
75         /* Do checksum on headers */
76         for (i =  0; i < 148 ; i++) {
77                 sum += tar.raw[i];
78         }
79         sum += ' ' * 8;
80         for (i =  156; i < 512 ; i++) {
81                 sum += tar.raw[i];
82         }
83         if (sum != strtol(tar.formated.chksum, NULL, 8)) {
84                 error_msg("Invalid tar header checksum");
85                 return(EXIT_FAILURE);
86         }
87
88         /* convert to type'ed variables */
89         if (tar.formated.prefix[0] == 0) {
90                 file_header->name = strdup(tar.formated.name);
91         } else {
92                 file_header->name = concat_path_file(tar.formated.prefix, tar.formated.name);
93         }
94         file_header->mode = strtol(tar.formated.mode, NULL, 8);
95         file_header->uid = strtol(tar.formated.uid, NULL, 8);
96         file_header->gid = strtol(tar.formated.gid, NULL, 8);
97         file_header->size = strtol(tar.formated.size, NULL, 8);
98         file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
99         file_header->link_name = (tar.formated.linkname[0] != '\0') ? 
100             xstrdup(tar.formated.linkname) : NULL;
101         file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
102                                  strtol(tar.formated.devminor, NULL, 8));
103
104 #if defined CONFIG_FEATURE_TAR_OLD_FORMAT || defined CONFIG_FEATURE_GNUTAR_LONG_FILENAME
105         /* Fix mode, used by the old format */
106         switch (tar.formated.typeflag) {
107 # ifdef CONFIG_FEATURE_TAR_OLD_FORMAT
108         case 0:
109                 file_header->mode |= S_IFREG;
110                 break;
111         case 1:
112                 error_msg("Internal hard link not supported");
113                 break;
114         case 2:
115                 file_header->mode |= S_IFLNK;
116                 break;
117         case 3:
118                 file_header->mode |= S_IFCHR;
119                 break;
120         case 4:
121                 file_header->mode |= S_IFBLK;
122                 break;
123         case 5:
124                 file_header->mode |= S_IFDIR;
125                 break;
126         case 6:
127                 file_header->mode |= S_IFIFO;
128                 break;
129 # endif
130 # ifdef CONFIG_FEATURE_GNUTAR_LONG_FILENAME
131         case 'L': {
132                         char *longname;
133
134                         longname = xmalloc(file_header->size + 1);
135                         xread_all(archive_handle->src_fd, longname, file_header->size);
136                         longname[file_header->size] = '\0';
137                         archive_handle->offset += file_header->size;
138
139                         get_header_tar(archive_handle);
140                         file_header->name = longname;
141                         break;
142                 }
143         case 'K': {
144                         char *linkname;
145
146                         linkname = xmalloc(file_header->size + 1);
147                         xread_all(archive_handle->src_fd, linkname, file_header->size);
148                         linkname[file_header->size] = '\0';
149                         archive_handle->offset += file_header->size;
150
151                         get_header_tar(archive_handle);
152                         file_header->name = linkname;
153                         break;
154                 }
155 # endif
156         }
157 #endif
158         if (archive_handle->filter(archive_handle->accept, archive_handle->reject, archive_handle->file_header->name) == EXIT_SUCCESS) {
159                 archive_handle->action_header(archive_handle->file_header);
160                 archive_handle->flags |= ARCHIVE_EXTRACT_QUIET;
161                 archive_handle->action_data(archive_handle);
162         } else {
163                 data_skip(archive_handle);                      
164         }
165         archive_handle->offset += file_header->size;
166
167         return(EXIT_SUCCESS);
168 }
169