1
2
3
4
5
6
7#include "libbb.h"
8#include "bb_archive.h"
9#include "ar.h"
10
11
12static unsigned read_num(char *str, int base, int len)
13{
14 int err;
15
16
17
18
19 str[len] = 0;
20
21
22
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
29char 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
38
39 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
40
41 return EXIT_FAILURE;
42 }
43
44
45
46
47 if (ar.raw[0] == '\n') {
48
49 memmove(ar.raw, &ar.raw[1], 59);
50 ar.raw[59] = xread_char(archive_handle->src_fd);
51 archive_handle->offset++;
52 }
53 archive_handle->offset += 60;
54
55 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
56 bb_error_msg_and_die("invalid ar header");
57
58
59
60
61
62
63 typed->size = size = read_num(ar.formatted.size, 10,
64 sizeof(ar.formatted.size));
65
66
67 if (ar.formatted.name[0] == '/') {
68 if (ar.formatted.name[1] == ' ') {
69
70 data_skip(archive_handle);
71 archive_handle->offset += size;
72 return get_header_ar(archive_handle);
73 }
74#if ENABLE_FEATURE_AR_LONG_FILENAMES
75 if (ar.formatted.name[1] == '/') {
76
77
78
79
80 archive_handle->ar__long_name_size = size;
81 free(archive_handle->ar__long_names);
82 archive_handle->ar__long_names = xzalloc(size + 1);
83 xread(archive_handle->src_fd, archive_handle->ar__long_names, size);
84 archive_handle->offset += size;
85
86 return get_header_ar(archive_handle);
87 }
88#else
89 bb_error_msg_and_die("long filenames not supported");
90#endif
91 }
92
93
94
95
96 typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode));
97 typed->gid = read_num(ar.formatted.gid, 10, sizeof(ar.formatted.gid));
98 typed->uid = read_num(ar.formatted.uid, 10, sizeof(ar.formatted.uid));
99 typed->mtime = read_num(ar.formatted.date, 10, sizeof(ar.formatted.date));
100
101#if ENABLE_FEATURE_AR_LONG_FILENAMES
102 if (ar.formatted.name[0] == '/') {
103 unsigned long_offset;
104
105
106
107 long_offset = read_num(&ar.formatted.name[1], 10,
108 sizeof(ar.formatted.name) - 1);
109 if (long_offset >= archive_handle->ar__long_name_size) {
110 bb_error_msg_and_die("can't resolve long filename");
111 }
112 typed->name = xstrdup(archive_handle->ar__long_names + long_offset);
113 } else
114#endif
115 {
116
117 typed->name = xstrndup(ar.formatted.name, 16);
118 }
119
120 typed->name[strcspn(typed->name, " /")] = '\0';
121
122 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
123 archive_handle->action_header(typed);
124#if ENABLE_DPKG || ENABLE_DPKG_DEB
125 if (archive_handle->dpkg__sub_archive) {
126 struct archive_handle_t *sa = archive_handle->dpkg__sub_archive;
127 while (archive_handle->dpkg__action_data_subarchive(sa) == EXIT_SUCCESS)
128 continue;
129 create_links_from_list(sa->link_placeholders);
130 } else
131#endif
132 archive_handle->action_data(archive_handle);
133 } else {
134 data_skip(archive_handle);
135 }
136
137 archive_handle->offset += typed->size;
138
139 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
140
141 return EXIT_SUCCESS;
142}
143