Don't delete source file when decompressing to stdout
[oweals/busybox.git] / archival / libunarchive / get_header_ar.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 file_header_t *get_header_ar(FILE *src_stream)
24 {
25         file_header_t *typed;
26         union {
27                 char raw[60];
28                 struct {
29                         char name[16];
30                         char date[12];
31                         char uid[6];
32                         char gid[6];
33                         char mode[8];
34                         char size[10];
35                         char magic[2];
36                 } formated;
37         } ar;
38         static char *ar_long_names;
39
40         if (fread(ar.raw, 1, 60, src_stream) != 60) {
41                 return(NULL);
42         }
43         archive_offset += 60;
44         /* align the headers based on the header magic */
45         if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
46                 /* some version of ar, have an extra '\n' after each data entry,
47                  * this puts the next header out by 1 */
48                 if (ar.formated.magic[1] != '`') {
49                         error_msg("Invalid magic");
50                         return(NULL);
51                 }
52                 /* read the next char out of what would be the data section,
53                  * if its a '\n' then it is a valid header offset by 1*/
54                 archive_offset++;
55                 if (fgetc(src_stream) != '\n') {
56                         error_msg("Invalid magic");
57                         return(NULL);
58                 }
59                 /* fix up the header, we started reading 1 byte too early */
60                 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
61                 memmove(ar.raw, &ar.raw[1], 59);
62         }
63                 
64         typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
65
66         typed->size = (size_t) atoi(ar.formated.size);
67         /* long filenames have '/' as the first character */
68         if (ar.formated.name[0] == '/') {
69                 if (ar.formated.name[1] == '/') {
70                         /* If the second char is a '/' then this entries data section
71                          * stores long filename for multiple entries, they are stored
72                          * in static variable long_names for use in future entries */
73                         ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
74                         fread(ar_long_names, 1, typed->size, src_stream);
75                         archive_offset += typed->size;
76                         /* This ar entries data section only contained filenames for other records
77                          * they are stored in the static ar_long_names for future reference */
78                         return (get_header_ar(src_stream)); /* Return next header */
79                 } else if (ar.formated.name[1] == ' ') {
80                         /* This is the index of symbols in the file for compilers */
81                         seek_sub_file(src_stream, typed->size);
82                         return (get_header_ar(src_stream)); /* Return next header */
83                 } else {
84                         /* The number after the '/' indicates the offset in the ar data section
85                         (saved in variable long_name) that conatains the real filename */
86                         if (!ar_long_names) {
87                                 error_msg("Cannot resolve long file name");
88                                 return (NULL);
89                         }
90                         typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
91                 }
92         } else {
93                 /* short filenames */
94                 typed->name = xcalloc(1, 16);
95                 strncpy(typed->name, ar.formated.name, 16);
96         }
97         typed->name[strcspn(typed->name, " /")]='\0';
98
99         /* convert the rest of the now valid char header to its typed struct */ 
100         parse_mode(ar.formated.mode, &typed->mode);
101         typed->mtime = atoi(ar.formated.date);
102         typed->uid = atoi(ar.formated.uid);
103         typed->gid = atoi(ar.formated.gid);
104
105         return(typed);
106 }