ar: read_num(): fix reading fields using the entire width
[oweals/busybox.git] / archival / libarchive / get_header_ar.c
1 /* vi: set sw=4 ts=4: */
2 /* Copyright 2001 Glenn McGrath.
3  *
4  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
5  */
6
7 #include "libbb.h"
8 #include "bb_archive.h"
9 #include "ar.h"
10
11 /* WARNING: Clobbers str[len], so fields must be read in reverse order! */
12 static unsigned read_num(char *str, int base, int len)
13 {
14         int err;
15
16         /* ar fields are fixed length text strings (padded with spaces).
17          * Ensure bb_strtou doesn't read past the field in case the full
18          * width is used. */
19         str[len] = 0;
20
21         /* This code works because
22          * on misformatted numbers bb_strtou returns all-ones */
23         err = bb_strtou(str, NULL, base);
24         if (err == -1)
25                 bb_error_msg_and_die("invalid ar header");
26         return err;
27 }
28
29 char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
30 {
31         file_header_t *typed = archive_handle->file_header;
32         unsigned size;
33         union {
34                 char raw[60];
35                 struct ar_header formatted;
36         } ar;
37 #if ENABLE_FEATURE_AR_LONG_FILENAMES
38         static char *ar_long_names;
39         static unsigned ar_long_name_size;
40 #endif
41
42         /* dont use xread as we want to handle the error ourself */
43         if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
44                 /* End Of File */
45                 return EXIT_FAILURE;
46         }
47
48         /* ar header starts on an even byte (2 byte aligned)
49          * '\n' is used for padding
50          */
51         if (ar.raw[0] == '\n') {
52                 /* fix up the header, we started reading 1 byte too early */
53                 memmove(ar.raw, &ar.raw[1], 59);
54                 ar.raw[59] = xread_char(archive_handle->src_fd);
55                 archive_handle->offset++;
56         }
57         archive_handle->offset += 60;
58
59         if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
60                 bb_error_msg_and_die("invalid ar header");
61
62         typed->size = size = read_num(ar.formatted.size, 10,
63                                       sizeof(ar.formatted.size));
64
65         /* special filenames have '/' as the first character */
66         if (ar.formatted.name[0] == '/') {
67                 if (ar.formatted.name[1] == ' ') {
68                         /* This is the index of symbols in the file for compilers */
69                         data_skip(archive_handle);
70                         archive_handle->offset += size;
71                         return get_header_ar(archive_handle); /* Return next header */
72                 }
73 #if ENABLE_FEATURE_AR_LONG_FILENAMES
74                 if (ar.formatted.name[1] == '/') {
75                         /* If the second char is a '/' then this entries data section
76                          * stores long filename for multiple entries, they are stored
77                          * in static variable long_names for use in future entries
78                          */
79                         ar_long_name_size = size;
80                         free(ar_long_names);
81                         ar_long_names = xmalloc(size);
82                         xread(archive_handle->src_fd, ar_long_names, size);
83                         archive_handle->offset += size;
84                         /* Return next header */
85                         return get_header_ar(archive_handle);
86                 }
87 #else
88                 bb_error_msg_and_die("long filenames not supported");
89 #endif
90         }
91         /* Only size is always present, the rest may be missing in
92          * long filename pseudo file. Thus we decode the rest
93          * after dealing with long filename pseudo file.
94          * Note that the fields MUST be read in reverse order as
95          * read_num() clobbers the next byte after the field!
96          */
97         typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode));
98         typed->gid = read_num(ar.formatted.gid, 10, sizeof(ar.formatted.gid));
99         typed->uid = read_num(ar.formatted.uid, 10, sizeof(ar.formatted.uid));
100         typed->mtime = read_num(ar.formatted.date, 10, sizeof(ar.formatted.date));
101
102 #if ENABLE_FEATURE_AR_LONG_FILENAMES
103         if (ar.formatted.name[0] == '/') {
104                 unsigned long_offset;
105
106                 /* The number after the '/' indicates the offset in the ar data section
107                  * (saved in ar_long_names) that conatains the real filename */
108                 long_offset = read_num(&ar.formatted.name[1], 10,
109                                        sizeof(ar.formatted.name) - 1);
110                 if (long_offset >= ar_long_name_size) {
111                         bb_error_msg_and_die("can't resolve long filename");
112                 }
113                 typed->name = xstrdup(ar_long_names + long_offset);
114         } else
115 #endif
116         {
117                 /* short filenames */
118                 typed->name = xstrndup(ar.formatted.name, 16);
119         }
120
121         typed->name[strcspn(typed->name, " /")] = '\0';
122
123         if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
124                 archive_handle->action_header(typed);
125 #if ENABLE_DPKG || ENABLE_DPKG_DEB
126                 if (archive_handle->dpkg__sub_archive) {
127                         while (archive_handle->dpkg__action_data_subarchive(archive_handle->dpkg__sub_archive) == EXIT_SUCCESS)
128                                 continue;
129                 } else
130 #endif
131                         archive_handle->action_data(archive_handle);
132         } else {
133                 data_skip(archive_handle);
134         }
135
136         archive_handle->offset += typed->size;
137         /* Set the file pointer to the correct spot, we may have been reading a compressed file */
138         lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
139
140         return EXIT_SUCCESS;
141 }