Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 23 | file_header_t *get_header_tar(FILE * tar_stream) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 24 | { |
| 25 | union { |
| 26 | unsigned char raw[512]; |
| 27 | struct { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 28 | char name[100]; /* 0-99 */ |
| 29 | char mode[8]; /* 100-107 */ |
| 30 | char uid[8]; /* 108-115 */ |
| 31 | char gid[8]; /* 116-123 */ |
| 32 | char size[12]; /* 124-135 */ |
| 33 | char mtime[12]; /* 136-147 */ |
| 34 | char chksum[8]; /* 148-155 */ |
| 35 | char typeflag; /* 156-156 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 36 | char linkname[100]; /* 157-256 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 37 | char magic[6]; /* 257-262 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 38 | char version[2]; /* 263-264 */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 39 | char uname[32]; /* 265-296 */ |
| 40 | char gname[32]; /* 297-328 */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 41 | char devmajor[8]; /* 329-336 */ |
| 42 | char devminor[8]; /* 337-344 */ |
| 43 | char prefix[155]; /* 345-499 */ |
| 44 | char padding[12]; /* 500-512 */ |
| 45 | } formated; |
| 46 | } tar; |
| 47 | file_header_t *tar_entry = NULL; |
| 48 | long sum = 0; |
| 49 | long i; |
| 50 | |
| 51 | if (archive_offset % 512 != 0) { |
| 52 | seek_sub_file(tar_stream, 512 - (archive_offset % 512)); |
| 53 | } |
| 54 | |
| 55 | if (fread(tar.raw, 1, 512, tar_stream) != 512) { |
| 56 | /* Unfortunatly its common for tar files to have all sorts of |
| 57 | * trailing garbage, fail silently */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 58 | return (NULL); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 59 | } |
| 60 | archive_offset += 512; |
| 61 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 62 | /* Check header has valid magic, "ustar" is for the proper tar |
| 63 | * 0's are for the old tar format |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 64 | */ |
| 65 | if (strncmp(tar.formated.magic, "ustar", 5) != 0) { |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 66 | #ifdef CONFIG_FEATURE_TAR_OLD_FORMAT |
| 67 | if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0) |
| 68 | #endif |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 69 | return (NULL); |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* If there is no filename its an empty header, skip it */ |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 73 | if (tar.formated.name[0] == 0) { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 74 | return (NULL); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* Do checksum on headers */ |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 78 | for (i = 0; i < 148; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 79 | sum += tar.raw[i]; |
| 80 | } |
| 81 | sum += ' ' * 8; |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 82 | for (i = 156; i < 512; i++) { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 83 | sum += tar.raw[i]; |
| 84 | } |
| 85 | if (sum != strtol(tar.formated.chksum, NULL, 8)) { |
| 86 | error_msg("Invalid tar header checksum"); |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 87 | return (NULL); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* convert to type'ed variables */ |
| 91 | tar_entry = xcalloc(1, sizeof(file_header_t)); |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 92 | if (tar.formated.prefix[0] == 0) { |
| 93 | tar_entry->name = xstrdup(tar.formated.name); |
| 94 | } else { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 95 | tar_entry->name = |
| 96 | concat_path_file(tar.formated.prefix, tar.formated.name); |
Glenn L McGrath | 7576270 | 2002-08-22 11:50:31 +0000 | [diff] [blame] | 97 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 98 | |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 99 | tar_entry->mode = strtol(tar.formated.mode, NULL, 8); |
| 100 | #ifdef CONFIG_FEATURE_TAR_OLD_FORMAT |
| 101 | switch (tar.formated.typeflag) { |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 102 | case 0: |
| 103 | tar_entry->mode |= S_IFREG; |
| 104 | break; |
| 105 | case 1: |
| 106 | error_msg("internal hard link not handled\n"); |
| 107 | break; |
| 108 | case 2: |
| 109 | tar_entry->mode |= S_IFLNK; |
| 110 | break; |
| 111 | case 3: |
| 112 | tar_entry->mode |= S_IFCHR; |
| 113 | break; |
| 114 | case 4: |
| 115 | tar_entry->mode |= S_IFBLK; |
| 116 | break; |
| 117 | case 5: |
| 118 | tar_entry->mode |= S_IFDIR; |
| 119 | break; |
| 120 | case 6: |
| 121 | tar_entry->mode |= S_IFIFO; |
| 122 | break; |
Glenn L McGrath | 1d23f3a | 2002-08-13 05:06:43 +0000 | [diff] [blame] | 123 | } |
| 124 | #endif |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 125 | tar_entry->uid = strtol(tar.formated.uid, NULL, 8); |
| 126 | tar_entry->gid = strtol(tar.formated.gid, NULL, 8); |
| 127 | tar_entry->size = strtol(tar.formated.size, NULL, 8); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 128 | tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8); |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 129 | tar_entry->link_name = |
| 130 | strlen(tar.formated.linkname) ? xstrdup(tar.formated.linkname) : NULL; |
| 131 | tar_entry->device = |
| 132 | (strtol(tar.formated.devmajor, NULL, 8) << 8) + |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 133 | strtol(tar.formated.devminor, NULL, 8); |
| 134 | |
Glenn L McGrath | 99b1254 | 2002-08-22 17:47:09 +0000 | [diff] [blame] | 135 | return (tar_entry); |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 136 | } |