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.
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.
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.
21 file_headers_t *get_tar_headers(FILE *tar_stream)
23 typedef struct raw_tar_header {
24 char name[100]; /* 0-99 */
25 char mode[8]; /* 100-107 */
26 char uid[8]; /* 108-115 */
27 char gid[8]; /* 116-123 */
28 char size[12]; /* 124-135 */
29 char mtime[12]; /* 136-147 */
30 char chksum[8]; /* 148-155 */
31 char typeflag; /* 156-156 */
32 char linkname[100]; /* 157-256 */
33 char magic[6]; /* 257-262 */
34 char version[2]; /* 263-264 */
35 char uname[32]; /* 265-296 */
36 char gname[32]; /* 297-328 */
37 char devmajor[8]; /* 329-336 */
38 char devminor[8]; /* 337-344 */
39 char prefix[155]; /* 345-499 */
40 char padding[12]; /* 500-512 */
43 raw_tar_header_t raw_tar_header;
44 unsigned char *temp = (unsigned char *) &raw_tar_header;
45 file_headers_t *tar_headers_list = (file_headers_t *) calloc(1, sizeof(file_headers_t));
46 file_headers_t *tar_entry;
48 long next_header_offset = 0;
49 long current_offset = 0;
51 while (fread((char *) &raw_tar_header, 1, 512, tar_stream) == 512) {
53 current_offset += 512;
55 /* Check header has valid magic, unfortunately some tar files
56 * have empty (0'ed) tar entries at the end, which will
57 * cause this to fail, so fail silently for now
59 if (strncmp(raw_tar_header.magic, "ustar", 5) != 0) {
60 // error_msg("invalid magic, [%s]\n", raw_tar_header.magic);
64 /* Do checksum on headers */
65 for (i = 0; i < 148 ; i++) {
69 for (i = 156; i < 512 ; i++) {
72 if (sum != strtol(raw_tar_header.chksum, NULL, 8)) {
73 error_msg("Invalid tar header checksum");
77 /* convert to type'ed variables */
78 tar_entry = xcalloc(1, sizeof(file_headers_t));
79 tar_entry->name = xstrdup(raw_tar_header.name);
81 tar_entry->offset = (off_t) current_offset;
82 parse_mode(raw_tar_header.mode, &tar_entry->mode);
83 tar_entry->uid = strtol(raw_tar_header.uid, NULL, 8);
84 tar_entry->gid = strtol(raw_tar_header.gid, NULL, 8);
85 tar_entry->size = strtol(raw_tar_header.size, NULL, 8);
86 tar_entry->mtime = strtol(raw_tar_header.mtime, NULL, 8);
87 tar_entry->link_name = xstrdup(raw_tar_header.linkname);
88 // tar_entry->devmajor = strtol(rawHeader->devmajor, NULL, 8);
89 // tar_entry->devminor = strtol(rawHeader->devminor, NULL, 8);
91 tar_headers_list = append_archive_list(tar_headers_list, tar_entry);
93 /* start of next header is at */
94 next_header_offset = current_offset + tar_entry->size;
96 if (tar_entry->size % 512 != 0) {
97 next_header_offset += (512 - tar_entry->size % 512);
100 * Seek to start of next block, cant use fseek as unzip() does support it
102 while (current_offset < next_header_offset) {
103 if (fgetc(tar_stream) == EOF) {
109 return(tar_headers_list);