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