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#if ENABLE_FEATURE_AR_LONG_FILENAMES
38 static char *ar_long_names;
39 static unsigned ar_long_name_size;
40#endif
41
42
43 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
44
45 return EXIT_FAILURE;
46 }
47
48
49
50
51 if (ar.raw[0] == '\n') {
52
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
63
64
65
66
67 typed->size = size = read_num(ar.formatted.size, 10,
68 sizeof(ar.formatted.size));
69
70
71 if (ar.formatted.name[0] == '/') {
72 if (ar.formatted.name[1] == ' ') {
73
74 data_skip(archive_handle);
75 archive_handle->offset += size;
76 return get_header_ar(archive_handle);
77 }
78#if ENABLE_FEATURE_AR_LONG_FILENAMES
79 if (ar.formatted.name[1] == '/') {
80
81
82
83
84 ar_long_name_size = size;
85 free(ar_long_names);
86 ar_long_names = xmalloc(size);
87 xread(archive_handle->src_fd, ar_long_names, size);
88 archive_handle->offset += size;
89
90 return get_header_ar(archive_handle);
91 }
92#else
93 bb_error_msg_and_die("long filenames not supported");
94#endif
95 }
96
97
98
99
100 typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode));
101 typed->gid = read_num(ar.formatted.gid, 10, sizeof(ar.formatted.gid));
102 typed->uid = read_num(ar.formatted.uid, 10, sizeof(ar.formatted.uid));
103 typed->mtime = read_num(ar.formatted.date, 10, sizeof(ar.formatted.date));
104
105#if ENABLE_FEATURE_AR_LONG_FILENAMES
106 if (ar.formatted.name[0] == '/') {
107 unsigned long_offset;
108
109
110
111 long_offset = read_num(&ar.formatted.name[1], 10,
112 sizeof(ar.formatted.name) - 1);
113 if (long_offset >= ar_long_name_size) {
114 bb_error_msg_and_die("can't resolve long filename");
115 }
116 typed->name = xstrdup(ar_long_names + long_offset);
117 } else
118#endif
119 {
120
121 typed->name = xstrndup(ar.formatted.name, 16);
122 }
123
124 typed->name[strcspn(typed->name, " /")] = '\0';
125
126 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
127 archive_handle->action_header(typed);
128#if ENABLE_DPKG || ENABLE_DPKG_DEB
129 if (archive_handle->dpkg__sub_archive) {
130 while (archive_handle->dpkg__action_data_subarchive(archive_handle->dpkg__sub_archive) == EXIT_SUCCESS)
131 continue;
132 } else
133#endif
134 archive_handle->action_data(archive_handle);
135 } else {
136 data_skip(archive_handle);
137 }
138
139 archive_handle->offset += typed->size;
140
141 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
142
143 return EXIT_SUCCESS;
144}
145