"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 06249fe | 2006-02-20 19:28:53 +0000 | [diff] [blame] | 2 | /* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 3 | * |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 4 | * FIXME: |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 5 | * In privileged mode if uname and gname map to a uid and gid then use the |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 6 | * mapped value instead of the uid/gid values in tar header |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 7 | * |
| 8 | * References: |
| 9 | * GNU tar and star man pages, |
| 10 | * Opengroup's ustar interchange format, |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 11 | * http://www.opengroup.org/onlinepubs/007904975/utilities/pax.html |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 15 | #include "unarchive.h" |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | ab6b446 | 2008-12-12 02:14:35 +0000 | [diff] [blame] | 17 | /* |
| 18 | * GNU tar uses "base-256 encoding" for very large numbers (>8 billion). |
| 19 | * Encoding is binary, with highest bit always set as a marker |
| 20 | * and sign in next-highest bit: |
| 21 | * 80 00 .. 00 - zero |
| 22 | * bf ff .. ff - largest positive number |
| 23 | * ff ff .. ff - minus 1 |
| 24 | * c0 00 .. 00 - smallest negative number |
| 25 | * |
| 26 | * We expect it only in size field, where negative numbers don't make sense. |
| 27 | */ |
| 28 | static off_t getBase256_len12(const char *str) |
| 29 | { |
| 30 | off_t value; |
| 31 | int len; |
| 32 | |
| 33 | /* if (*str & 0x40) error; - caller prevents this */ |
| 34 | |
| 35 | if (sizeof(off_t) >= 12) { |
| 36 | /* Probably 128-bit (16 byte) off_t. Can be optimized. */ |
| 37 | len = 12; |
| 38 | value = *str++ & 0x3f; |
| 39 | while (--len) |
| 40 | value = (value << 8) + (unsigned char) *str++; |
| 41 | return value; |
| 42 | } |
| 43 | |
| 44 | #ifdef CHECK_FOR_OVERFLOW |
| 45 | /* Can be optimized to eat 32-bit chunks */ |
| 46 | char c = *str++ & 0x3f; |
| 47 | len = 12; |
| 48 | while (1) { |
| 49 | if (c) |
| 50 | bb_error_msg_and_die("overflow in base-256 encoded file size"); |
Denis Vlasenko | 3266aa9 | 2009-04-01 11:24:04 +0000 | [diff] [blame] | 51 | if (--len == sizeof(off_t)) |
Denis Vlasenko | ab6b446 | 2008-12-12 02:14:35 +0000 | [diff] [blame] | 52 | break; |
| 53 | c = *str++; |
| 54 | } |
| 55 | #else |
| 56 | str += (12 - sizeof(off_t)); |
| 57 | #endif |
| 58 | |
| 59 | /* Now str points to sizeof(off_t) least significant bytes. |
| 60 | * |
| 61 | * Example of tar file with 8914993153 (0x213600001) byte file. |
| 62 | * Field starts at offset 7c: |
| 63 | * 00070 30 30 30 00 30 30 30 30 30 30 30 00 80 00 00 00 |000.0000000.....| |
| 64 | * 00080 00 00 00 02 13 60 00 01 31 31 31 32 30 33 33 36 |.....`..11120336| |
| 65 | * |
| 66 | * str is at offset 80 or 84 now (64-bit or 32-bit off_t). |
| 67 | * We (ab)use the fact that value happens to be aligned, |
| 68 | * and fetch it in one go: |
| 69 | */ |
| 70 | if (sizeof(off_t) == 8) { |
| 71 | value = *(off_t*)str; |
| 72 | value = SWAP_BE64(value); |
| 73 | } else if (sizeof(off_t) == 4) { |
| 74 | value = *(off_t*)str; |
| 75 | value = SWAP_BE32(value); |
| 76 | } else { |
| 77 | value = 0; |
| 78 | len = sizeof(off_t); |
| 79 | while (--len) |
| 80 | value = (value << 8) + (unsigned char) *str++; |
| 81 | } |
| 82 | return value; |
| 83 | } |
| 84 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 85 | /* NB: _DESTROYS_ str[len] character! */ |
| 86 | static unsigned long long getOctal(char *str, int len) |
| 87 | { |
| 88 | unsigned long long v; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 89 | /* NB: leading spaces are allowed. Using strtoull to handle that. |
Denys Vlasenko | 8d33817 | 2009-09-23 17:16:37 +0200 | [diff] [blame] | 90 | * The downside is that we accept e.g. "-123" too :( |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 91 | */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 92 | str[len] = '\0'; |
| 93 | v = strtoull(str, &str, 8); |
Denys Vlasenko | 8d33817 | 2009-09-23 17:16:37 +0200 | [diff] [blame] | 94 | /* std: "Each numeric field is terminated by one or more |
| 95 | * <space> or NUL characters". We must support ' '! */ |
| 96 | if (*str != '\0' && *str != ' ') |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 97 | bb_error_msg_and_die("corrupted octal value in tar header"); |
| 98 | return v; |
| 99 | } |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 100 | #define GET_OCTAL(a) getOctal((a), sizeof(a)) |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 101 | |
| 102 | void BUG_tar_header_size(void); |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 103 | char FAST_FUNC get_header_tar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 104 | { |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 105 | file_header_t *file_header = archive_handle->file_header; |
| 106 | struct { |
| 107 | /* ustar header, Posix 1003.1 */ |
| 108 | char name[100]; /* 0-99 */ |
| 109 | char mode[8]; /* 100-107 */ |
| 110 | char uid[8]; /* 108-115 */ |
| 111 | char gid[8]; /* 116-123 */ |
| 112 | char size[12]; /* 124-135 */ |
| 113 | char mtime[12]; /* 136-147 */ |
| 114 | char chksum[8]; /* 148-155 */ |
| 115 | char typeflag; /* 156-156 */ |
| 116 | char linkname[100]; /* 157-256 */ |
Denis Vlasenko | 8512862 | 2007-11-16 20:35:30 +0000 | [diff] [blame] | 117 | /* POSIX: "ustar" NUL "00" */ |
| 118 | /* GNU tar: "ustar " NUL */ |
Paul Fox | 16aec39 | 2007-11-17 19:11:05 +0000 | [diff] [blame] | 119 | /* Normally it's defined as magic[6] followed by |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 120 | * version[2], but we put them together to simplify code |
Paul Fox | 16aec39 | 2007-11-17 19:11:05 +0000 | [diff] [blame] | 121 | */ |
Denis Vlasenko | 8512862 | 2007-11-16 20:35:30 +0000 | [diff] [blame] | 122 | char magic[8]; /* 257-264 */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 123 | char uname[32]; /* 265-296 */ |
| 124 | char gname[32]; /* 297-328 */ |
| 125 | char devmajor[8]; /* 329-336 */ |
| 126 | char devminor[8]; /* 337-344 */ |
| 127 | char prefix[155]; /* 345-499 */ |
| 128 | char padding[12]; /* 500-512 */ |
| 129 | } tar; |
| 130 | char *cp; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 131 | int i, sum_u, sum; |
| 132 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
| 133 | int sum_s; |
| 134 | #endif |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 135 | int parse_names; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 137 | /* Our "private data" */ |
| 138 | #define p_end (*(smallint *)(&archive_handle->ah_priv[0])) |
| 139 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
| 140 | #define p_longname (*(char* *)(&archive_handle->ah_priv[1])) |
| 141 | #define p_linkname (*(char* *)(&archive_handle->ah_priv[2])) |
| 142 | #else |
| 143 | #define p_longname 0 |
| 144 | #define p_linkname 0 |
| 145 | #endif |
| 146 | // if (!archive_handle->ah_priv_inited) { |
| 147 | // archive_handle->ah_priv_inited = 1; |
| 148 | // p_end = 0; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 149 | // IF_FEATURE_TAR_GNU_EXTENSIONS(p_longname = NULL;) |
| 150 | // IF_FEATURE_TAR_GNU_EXTENSIONS(p_linkname = NULL;) |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 151 | // } |
| 152 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 153 | if (sizeof(tar) != 512) |
| 154 | BUG_tar_header_size(); |
| 155 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 156 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
| 157 | again: |
| 158 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 159 | /* Align header */ |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 160 | data_align(archive_handle, 512); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 162 | again_after_align: |
| 163 | |
Denis Vlasenko | 1f0b95f | 2009-03-13 14:26:44 +0000 | [diff] [blame] | 164 | #if ENABLE_DESKTOP || ENABLE_FEATURE_TAR_AUTODETECT |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 165 | /* to prevent misdetection of bz2 sig */ |
| 166 | *(uint32_t*)(&tar) = 0; |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 167 | i = full_read(archive_handle->src_fd, &tar, 512); |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 168 | /* If GNU tar sees EOF in above read, it says: |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 169 | * "tar: A lone zero block at N", where N = kilobyte |
| 170 | * where EOF was met (not EOF block, actual EOF!), |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 171 | * and exits with EXIT_SUCCESS. |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 172 | * We will mimic exit(EXIT_SUCCESS), although we will not mimic |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 173 | * the message and we don't check whether we indeed |
| 174 | * saw zero block directly before this. */ |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 175 | if (i == 0) { |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 176 | xfunc_error_retval = 0; |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 177 | short_read: |
Denis Vlasenko | 23ffb6a | 2008-02-13 17:52:42 +0000 | [diff] [blame] | 178 | bb_error_msg_and_die("short read"); |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 179 | } |
| 180 | if (i != 512) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 181 | IF_FEATURE_TAR_AUTODETECT(goto autodetect;) |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 182 | goto short_read; |
| 183 | } |
| 184 | |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 185 | #else |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 186 | i = 512; |
| 187 | xread(archive_handle->src_fd, &tar, i); |
Denis Vlasenko | 12c0622 | 2008-02-14 08:52:30 +0000 | [diff] [blame] | 188 | #endif |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 189 | archive_handle->offset += i; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 190 | |
| 191 | /* If there is no filename its an empty header */ |
Denis Vlasenko | 05efca9 | 2008-04-29 04:12:58 +0000 | [diff] [blame] | 192 | if (tar.name[0] == 0 && tar.prefix[0] == 0) { |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 193 | if (p_end) { |
| 194 | /* Second consecutive empty header - end of archive. |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 195 | * Read until the end to empty the pipe from gz or bz2 |
| 196 | */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 197 | while (full_read(archive_handle->src_fd, &tar, 512) == 512) |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 198 | continue; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 199 | return EXIT_FAILURE; |
Paul Fox | 94ff9f1 | 2005-07-20 19:24:13 +0000 | [diff] [blame] | 200 | } |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 201 | p_end = 1; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 202 | return EXIT_SUCCESS; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 203 | } |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 204 | p_end = 0; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 205 | |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 206 | /* Check header has valid magic, "ustar" is for the proper tar, |
| 207 | * five NULs are for the old tar format */ |
| 208 | if (strncmp(tar.magic, "ustar", 5) != 0 |
| 209 | && (!ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY |
| 210 | || memcmp(tar.magic, "\0\0\0\0", 5) != 0) |
| 211 | ) { |
| 212 | #if ENABLE_FEATURE_TAR_AUTODETECT |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 213 | char FAST_FUNC (*get_header_ptr)(archive_handle_t *); |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 214 | |
Denis Vlasenko | 1f0b95f | 2009-03-13 14:26:44 +0000 | [diff] [blame] | 215 | autodetect: |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 216 | /* tar gz/bz autodetect: check for gz/bz2 magic. |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 217 | * If we see the magic, and it is the very first block, |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 218 | * we can switch to get_header_tar_gz/bz2/lzma(). |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 219 | * Needs seekable fd. I wish recv(MSG_PEEK) works |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 220 | * on any fd... */ |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 221 | #if ENABLE_FEATURE_SEAMLESS_GZ |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 222 | if (tar.name[0] == 0x1f && tar.name[1] == (char)0x8b) { /* gzip */ |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 223 | get_header_ptr = get_header_tar_gz; |
| 224 | } else |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 225 | #endif |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 226 | #if ENABLE_FEATURE_SEAMLESS_BZ2 |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 227 | if (tar.name[0] == 'B' && tar.name[1] == 'Z' |
| 228 | && tar.name[2] == 'h' && isdigit(tar.name[3]) |
| 229 | ) { /* bzip2 */ |
| 230 | get_header_ptr = get_header_tar_bz2; |
| 231 | } else |
| 232 | #endif |
| 233 | goto err; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 234 | /* Two different causes for lseek() != 0: |
| 235 | * unseekable fd (would like to support that too, but...), |
| 236 | * or not first block (false positive, it's not .gz/.bz2!) */ |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 237 | if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0) |
Denis Vlasenko | 431a7c9 | 2008-02-19 11:26:28 +0000 | [diff] [blame] | 238 | goto err; |
| 239 | while (get_header_ptr(archive_handle) == EXIT_SUCCESS) |
| 240 | continue; |
| 241 | return EXIT_FAILURE; |
| 242 | err: |
| 243 | #endif /* FEATURE_TAR_AUTODETECT */ |
| 244 | bb_error_msg_and_die("invalid tar magic"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 245 | } |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 246 | |
| 247 | /* Do checksum on headers. |
| 248 | * POSIX says that checksum is done on unsigned bytes, but |
Denis Vlasenko | 940494f | 2007-03-04 18:09:50 +0000 | [diff] [blame] | 249 | * Sun and HP-UX gets it wrong... more details in |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 250 | * GNU tar source. */ |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 251 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
| 252 | sum_s = ' ' * sizeof(tar.chksum); |
| 253 | #endif |
| 254 | sum_u = ' ' * sizeof(tar.chksum); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 255 | for (i = 0; i < 148; i++) { |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 256 | sum_u += ((unsigned char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 257 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 258 | sum_s += ((signed char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 259 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 260 | } |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 261 | for (i = 156; i < 512; i++) { |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 262 | sum_u += ((unsigned char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 263 | #if ENABLE_FEATURE_TAR_OLDSUN_COMPATIBILITY |
Denis Vlasenko | dcbd51d | 2007-03-03 20:06:59 +0000 | [diff] [blame] | 264 | sum_s += ((signed char*)&tar)[i]; |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 265 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 266 | } |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 267 | /* This field does not need special treatment (getOctal) */ |
Denys Vlasenko | 8d33817 | 2009-09-23 17:16:37 +0200 | [diff] [blame] | 268 | { |
| 269 | char *endp; /* gcc likes temp var for &endp */ |
| 270 | sum = strtoul(tar.chksum, &endp, 8); |
| 271 | if ((*endp != '\0' && *endp != ' ') |
| 272 | || (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) |
| 273 | ) { |
| 274 | bb_error_msg_and_die("invalid tar header checksum"); |
| 275 | } |
| 276 | } |
Denys Vlasenko | d645968 | 2010-01-03 23:41:11 +0100 | [diff] [blame] | 277 | /* don't use xstrtoul, tar.chksum may have leading spaces */ |
| 278 | sum = strtoul(tar.chksum, NULL, 8); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 279 | if (sum_u != sum IF_FEATURE_TAR_OLDSUN_COMPATIBILITY(&& sum_s != sum)) { |
Denis Vlasenko | a80b4a0 | 2007-06-21 12:41:59 +0000 | [diff] [blame] | 280 | bb_error_msg_and_die("invalid tar header checksum"); |
| 281 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 282 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 283 | /* 0 is reserved for high perf file, treat as normal file */ |
| 284 | if (!tar.typeflag) tar.typeflag = '0'; |
| 285 | parse_names = (tar.typeflag >= '0' && tar.typeflag <= '7'); |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 286 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 287 | /* getOctal trashes subsequent field, therefore we call it |
| 288 | * on fields in reverse order */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 289 | if (tar.devmajor[0]) { |
Denis Vlasenko | d93400b | 2008-04-29 03:54:16 +0000 | [diff] [blame] | 290 | char t = tar.prefix[0]; |
| 291 | /* we trash prefix[0] here, but we DO need it later! */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 292 | unsigned minor = GET_OCTAL(tar.devminor); |
| 293 | unsigned major = GET_OCTAL(tar.devmajor); |
| 294 | file_header->device = makedev(major, minor); |
Denis Vlasenko | d93400b | 2008-04-29 03:54:16 +0000 | [diff] [blame] | 295 | tar.prefix[0] = t; |
Denis Vlasenko | cba9ef5 | 2006-10-10 21:00:47 +0000 | [diff] [blame] | 296 | } |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 297 | file_header->link_target = NULL; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 298 | if (!p_linkname && parse_names && tar.linkname[0]) { |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 299 | file_header->link_target = xstrndup(tar.linkname, sizeof(tar.linkname)); |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 300 | /* FIXME: what if we have non-link object with link_target? */ |
| 301 | /* Will link_target be free()ed? */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 302 | } |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 303 | #if ENABLE_FEATURE_TAR_UNAME_GNAME |
| 304 | file_header->uname = tar.uname[0] ? xstrndup(tar.uname, sizeof(tar.uname)) : NULL; |
| 305 | file_header->gname = tar.gname[0] ? xstrndup(tar.gname, sizeof(tar.gname)) : NULL; |
| 306 | #endif |
Denys Vlasenko | b773f71 | 2009-07-08 18:58:09 +0200 | [diff] [blame] | 307 | /* mtime: rudimentally handle GNU tar's "base256 encoding" |
| 308 | * People report tarballs with NEGATIVE unix times encoded that way */ |
| 309 | file_header->mtime = (tar.mtime[0] & 0x80) /* base256? */ |
| 310 | ? 0 /* bogus */ |
| 311 | : GET_OCTAL(tar.mtime); |
| 312 | /* size: handle GNU tar's "base256 encoding" */ |
| 313 | file_header->size = (tar.size[0] & 0xc0) == 0x80 /* positive base256? */ |
Denis Vlasenko | ab6b446 | 2008-12-12 02:14:35 +0000 | [diff] [blame] | 314 | ? getBase256_len12(tar.size) |
| 315 | : GET_OCTAL(tar.size); |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 316 | file_header->gid = GET_OCTAL(tar.gid); |
| 317 | file_header->uid = GET_OCTAL(tar.uid); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 318 | /* Set bits 0-11 of the files mode */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 319 | file_header->mode = 07777 & GET_OCTAL(tar.mode); |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 320 | |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 321 | file_header->name = NULL; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 322 | if (!p_longname && parse_names) { |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 323 | /* we trash mode[0] here, it's ok */ |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 324 | //tar.name[sizeof(tar.name)] = '\0'; - gcc 4.3.0 would complain |
| 325 | tar.mode[0] = '\0'; |
Denis Vlasenko | 87cd4a8 | 2006-11-25 23:47:32 +0000 | [diff] [blame] | 326 | if (tar.prefix[0]) { |
| 327 | /* and padding[0] */ |
Denis Vlasenko | bc1918a | 2008-04-15 01:17:50 +0000 | [diff] [blame] | 328 | //tar.prefix[sizeof(tar.prefix)] = '\0'; - gcc 4.3.0 would complain |
| 329 | tar.padding[0] = '\0'; |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 330 | file_header->name = concat_path_file(tar.prefix, tar.name); |
Denis Vlasenko | 87cd4a8 | 2006-11-25 23:47:32 +0000 | [diff] [blame] | 331 | } else |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 332 | file_header->name = xstrdup(tar.name); |
| 333 | } |
| 334 | |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 335 | /* Set bits 12-15 of the files mode */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 336 | /* (typeflag was not trashed because chksum does not use getOctal) */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 337 | switch (tar.typeflag) { |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 338 | /* busybox identifies hard links as being regular files with 0 size and a link name */ |
| 339 | case '1': |
Glenn L McGrath | 916ba53 | 2004-02-20 02:34:42 +0000 | [diff] [blame] | 340 | file_header->mode |= S_IFREG; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 341 | break; |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 342 | case '7': |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 343 | /* case 0: */ |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 344 | case '0': |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 345 | #if ENABLE_FEATURE_TAR_OLDGNU_COMPATIBILITY |
Glenn L McGrath | 87af49f | 2003-09-09 17:41:03 +0000 | [diff] [blame] | 346 | if (last_char_is(file_header->name, '/')) { |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 347 | goto set_dir; |
| 348 | } |
Glenn L McGrath | c9f1fce | 2004-02-20 02:25:18 +0000 | [diff] [blame] | 349 | #endif |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 350 | file_header->mode |= S_IFREG; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 351 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 352 | case '2': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 353 | file_header->mode |= S_IFLNK; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 354 | /* have seen tarballs with size field containing |
| 355 | * the size of the link target's name */ |
| 356 | size0: |
| 357 | file_header->size = 0; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 358 | break; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 359 | case '3': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 360 | file_header->mode |= S_IFCHR; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 361 | goto size0; /* paranoia */ |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 362 | case '4': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 363 | file_header->mode |= S_IFBLK; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 364 | goto size0; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 365 | case '5': |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 366 | IF_FEATURE_TAR_OLDGNU_COMPATIBILITY(set_dir:) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 367 | file_header->mode |= S_IFDIR; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 368 | goto size0; |
Glenn L McGrath | 21110a0 | 2003-01-28 01:45:48 +0000 | [diff] [blame] | 369 | case '6': |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 370 | file_header->mode |= S_IFIFO; |
Denis Vlasenko | adc772a | 2008-07-20 17:10:43 +0000 | [diff] [blame] | 371 | goto size0; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 372 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 373 | case 'L': |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 374 | /* free: paranoia: tar with several consecutive longnames */ |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 375 | free(p_longname); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 376 | /* For paranoia reasons we allocate extra NUL char */ |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 377 | p_longname = xzalloc(file_header->size + 1); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 378 | /* We read ASCIZ string, including NUL */ |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 379 | xread(archive_handle->src_fd, p_longname, file_header->size); |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 380 | archive_handle->offset += file_header->size; |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 381 | /* return get_header_tar(archive_handle); */ |
| 382 | /* gcc 4.1.1 didn't optimize it into jump */ |
| 383 | /* so we will do it ourself, this also saves stack */ |
| 384 | goto again; |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 385 | case 'K': |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 386 | free(p_linkname); |
| 387 | p_linkname = xzalloc(file_header->size + 1); |
| 388 | xread(archive_handle->src_fd, p_linkname, file_header->size); |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 389 | archive_handle->offset += file_header->size; |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 390 | /* return get_header_tar(archive_handle); */ |
| 391 | goto again; |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 392 | case 'D': /* GNU dump dir */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 393 | case 'M': /* Continuation of multi volume archive */ |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 394 | case 'N': /* Old GNU for names > 100 characters */ |
| 395 | case 'S': /* Sparse file */ |
| 396 | case 'V': /* Volume header */ |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 397 | #endif |
Rob Landley | ff6e21c | 2006-07-06 20:30:19 +0000 | [diff] [blame] | 398 | case 'g': /* pax global header */ |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 399 | case 'x': { /* pax extended header */ |
| 400 | off_t sz; |
| 401 | bb_error_msg("warning: skipping header '%c'", tar.typeflag); |
| 402 | sz = (file_header->size + 511) & ~(off_t)511; |
| 403 | archive_handle->offset += sz; |
| 404 | sz >>= 9; /* sz /= 512 but w/o contortions for signed div */ |
| 405 | while (sz--) |
| 406 | xread(archive_handle->src_fd, &tar, 512); |
| 407 | /* return get_header_tar(archive_handle); */ |
| 408 | goto again_after_align; |
| 409 | } |
Glenn L McGrath | b0e163a | 2004-02-19 08:48:30 +0000 | [diff] [blame] | 410 | default: |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 411 | bb_error_msg_and_die("unknown typeflag: 0x%x", tar.typeflag); |
Glenn L McGrath | 6aa5223 | 2004-02-17 11:55:06 +0000 | [diff] [blame] | 412 | } |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 413 | |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 414 | #if ENABLE_FEATURE_TAR_GNU_EXTENSIONS |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 415 | if (p_longname) { |
| 416 | file_header->name = p_longname; |
| 417 | p_longname = NULL; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 418 | } |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 419 | if (p_linkname) { |
| 420 | file_header->link_target = p_linkname; |
| 421 | p_linkname = NULL; |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 422 | } |
| 423 | #endif |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 424 | if (strncmp(file_header->name, "/../"+1, 3) == 0 |
Denis Vlasenko | c1660fe | 2006-11-26 15:42:03 +0000 | [diff] [blame] | 425 | || strstr(file_header->name, "/../") |
| 426 | ) { |
| 427 | bb_error_msg_and_die("name with '..' encountered: '%s'", |
| 428 | file_header->name); |
| 429 | } |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 430 | |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 431 | /* Strip trailing '/' in directories */ |
Denis Vlasenko | 714701c | 2006-12-22 00:21:07 +0000 | [diff] [blame] | 432 | /* Must be done after mode is set as '/' is used to check if it's a directory */ |
Denis Vlasenko | 376ce1e | 2006-11-24 14:51:01 +0000 | [diff] [blame] | 433 | cp = last_char_is(file_header->name, '/'); |
Glenn L McGrath | 3d5828f | 2003-08-14 02:55:15 +0000 | [diff] [blame] | 434 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 435 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 436 | archive_handle->action_header(/*archive_handle->*/ file_header); |
Denis Vlasenko | cf30cc8 | 2006-11-24 14:53:18 +0000 | [diff] [blame] | 437 | /* Note that we kill the '/' only after action_header() */ |
| 438 | /* (like GNU tar 1.15.1: verbose mode outputs "dir/dir/") */ |
| 439 | if (cp) *cp = '\0'; |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 440 | archive_handle->ah_flags |= ARCHIVE_EXTRACT_QUIET; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 441 | archive_handle->action_data(archive_handle); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 442 | llist_add_to(&(archive_handle->passed), file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 443 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 444 | data_skip(archive_handle); |
Denis Vlasenko | b596335 | 2006-11-26 01:46:59 +0000 | [diff] [blame] | 445 | free(file_header->name); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 446 | } |
| 447 | archive_handle->offset += file_header->size; |
| 448 | |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 449 | free(file_header->link_target); |
Denis Vlasenko | a46dd89 | 2008-07-12 09:20:44 +0000 | [diff] [blame] | 450 | /* Do not free(file_header->name)! (why?) */ |
Denis Vlasenko | e00e502 | 2008-02-14 20:37:54 +0000 | [diff] [blame] | 451 | #if ENABLE_FEATURE_TAR_UNAME_GNAME |
| 452 | free(file_header->uname); |
| 453 | free(file_header->gname); |
| 454 | #endif |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 455 | return EXIT_SUCCESS; |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 456 | } |