1 /* vi: set sw=4 ts=4: */
2 /* Copyright 2001 Glenn McGrath.
4 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include "unarchive.h"
14 char get_header_ar(archive_handle_t *archive_handle)
16 file_header_t *typed = archive_handle->file_header;
29 #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
30 static char *ar_long_names;
31 static unsigned int ar_long_name_size;
34 /* dont use bb_xread as we want to handle the error ourself */
35 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
40 /* ar header starts on an even byte (2 byte aligned)
41 * '\n' is used for padding
43 if (ar.raw[0] == '\n') {
44 /* fix up the header, we started reading 1 byte too early */
45 memmove(ar.raw, &ar.raw[1], 59);
46 ar.raw[59] = xread_char(archive_handle->src_fd);
47 archive_handle->offset++;
49 archive_handle->offset += 60;
51 /* align the headers based on the header magic */
52 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
53 bb_error_msg_and_die("Invalid ar header");
56 typed->mode = strtol(ar.formated.mode, NULL, 8);
57 typed->mtime = atoi(ar.formated.date);
58 typed->uid = atoi(ar.formated.uid);
59 typed->gid = atoi(ar.formated.gid);
60 typed->size = atoi(ar.formated.size);
62 /* long filenames have '/' as the first character */
63 if (ar.formated.name[0] == '/') {
64 #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
65 if (ar.formated.name[1] == '/') {
66 /* If the second char is a '/' then this entries data section
67 * stores long filename for multiple entries, they are stored
68 * in static variable long_names for use in future entries */
69 ar_long_name_size = typed->size;
70 ar_long_names = xmalloc(ar_long_name_size);
71 xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
72 archive_handle->offset += ar_long_name_size;
73 /* This ar entries data section only contained filenames for other records
74 * they are stored in the static ar_long_names for future reference */
75 return (get_header_ar(archive_handle)); /* Return next header */
76 } else if (ar.formated.name[1] == ' ') {
77 /* This is the index of symbols in the file for compilers */
78 data_skip(archive_handle);
79 archive_handle->offset += typed->size;
80 return (get_header_ar(archive_handle)); /* Return next header */
82 /* The number after the '/' indicates the offset in the ar data section
83 (saved in variable long_name) that conatains the real filename */
84 const unsigned int long_offset = atoi(&ar.formated.name[1]);
85 if (long_offset >= ar_long_name_size) {
86 bb_error_msg_and_die("Cant resolve long filename");
88 typed->name = bb_xstrdup(ar_long_names + long_offset);
91 bb_error_msg_and_die("long filenames not supported");
95 typed->name = bb_xstrndup(ar.formated.name, 16);
98 typed->name[strcspn(typed->name, " /")] = '\0';
100 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
101 archive_handle->action_header(typed);
102 if (archive_handle->sub_archive) {
103 while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS);
105 archive_handle->action_data(archive_handle);
108 data_skip(archive_handle);
111 archive_handle->offset += typed->size;
112 /* Set the file pointer to the correct spot, we may have been reading a compressed file */
113 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
115 return(EXIT_SUCCESS);