1 /* vi: set sw=4 ts=4: */
3 * Copyright 2001 Glenn McGrath.
5 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 #include "bb_archive.h"
11 /* WARNING: Clobbers str[len], so fields must be read in reverse order! */
12 static unsigned read_num(char *str, int base, int len)
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
21 /* This code works because
22 * on misformatted numbers bb_strtou returns all-ones */
23 err = bb_strtou(str, NULL, base);
25 bb_simple_error_msg_and_die("invalid ar header");
29 char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
31 file_header_t *typed = archive_handle->file_header;
35 struct ar_header formatted;
38 /* dont use xread as we want to handle the error ourself */
39 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
44 /* ar header starts on an even byte (2 byte aligned)
45 * '\n' is used for padding
47 if (ar.raw[0] == '\n') {
48 /* fix up the header, we started reading 1 byte too early */
49 memmove(ar.raw, &ar.raw[1], 59);
50 ar.raw[59] = xread_char(archive_handle->src_fd);
51 archive_handle->offset++;
53 archive_handle->offset += 60;
55 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
56 bb_simple_error_msg_and_die("invalid ar header");
59 * Note that the fields MUST be read in reverse order as
60 * read_num() clobbers the next byte after the field!
61 * Order is: name, date, uid, gid, mode, size, magic.
63 typed->size = size = read_num(ar.formatted.size, 10,
64 sizeof(ar.formatted.size));
66 /* special filenames have '/' as the first character */
67 if (ar.formatted.name[0] == '/') {
68 if (ar.formatted.name[1] == ' ') {
69 /* This is the index of symbols in the file for compilers */
70 data_skip(archive_handle);
71 archive_handle->offset += size;
72 return get_header_ar(archive_handle); /* Return next header */
74 #if ENABLE_FEATURE_AR_LONG_FILENAMES
75 if (ar.formatted.name[1] == '/') {
76 /* If the second char is a '/' then this entries data section
77 * stores long filename for multiple entries, they are stored
78 * in static variable long_names for use in future entries
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 /* Return next header */
86 return get_header_ar(archive_handle);
89 bb_simple_error_msg_and_die("long filenames not supported");
92 /* Only size is always present, the rest may be missing in
93 * long filename pseudo file. Thus we decode the rest
94 * after dealing with long filename pseudo file.
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));
101 #if ENABLE_FEATURE_AR_LONG_FILENAMES
102 if (ar.formatted.name[0] == '/') {
103 unsigned long_offset;
105 /* The number after the '/' indicates the offset in the ar data section
106 * (saved in ar__long_names) that contains the real filename */
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_simple_error_msg_and_die("can't resolve long filename");
112 typed->name = xstrdup(archive_handle->ar__long_names + long_offset);
116 /* short filenames */
117 typed->name = xstrndup(ar.formatted.name, 16);
120 typed->name[strcspn(typed->name, " /")] = '\0';
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)
129 create_links_from_list(sa->link_placeholders);
132 archive_handle->action_data(archive_handle);
134 data_skip(archive_handle);
137 archive_handle->offset += typed->size;
138 /* Set the file pointer to the correct spot, we may have been reading a compressed file */
139 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);