blob: 59fd34c732965a03af23e040602bc5ee80db0f00 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley1ec5b292006-05-29 07:42:02 +00002/* Copyright 2001 Glenn McGrath.
Glenn L McGrath95ebf612001-10-25 14:18:08 +00003 *
Rob Landley1ec5b292006-05-29 07:42:02 +00004 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Glenn L McGrath95ebf612001-10-25 14:18:08 +00005 */
6
Glenn L McGrath95ebf612001-10-25 14:18:08 +00007#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +00008#include "unarchive.h"
Glenn L McGrath95ebf612001-10-25 14:18:08 +00009
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000010char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000011{
Denis Vlasenko666da5e2006-12-26 18:17:42 +000012 int err;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000013 file_header_t *typed = archive_handle->file_header;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000014 union {
15 char raw[60];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000016 struct {
17 char name[16];
18 char date[12];
19 char uid[6];
20 char gid[6];
21 char mode[8];
22 char size[10];
23 char magic[2];
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000024 } formatted;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000025 } ar;
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000026#if ENABLE_FEATURE_AR_LONG_FILENAMES
Glenn L McGrath95ebf612001-10-25 14:18:08 +000027 static char *ar_long_names;
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000028 static unsigned ar_long_name_size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000029#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +000030
Rob Landleyd921b2e2006-08-03 15:41:12 +000031 /* dont use xread as we want to handle the error ourself */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000032 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
33 /* End Of File */
Denis Vlasenko079f8af2006-11-27 16:49:31 +000034 return EXIT_FAILURE;
Glenn L McGrath91e46462003-07-31 01:53:50 +000035 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000036
Glenn L McGrath958ac182004-04-09 06:59:05 +000037 /* ar header starts on an even byte (2 byte aligned)
38 * '\n' is used for padding
39 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000040 if (ar.raw[0] == '\n') {
41 /* fix up the header, we started reading 1 byte too early */
42 memmove(ar.raw, &ar.raw[1], 59);
Rob Landley53437472006-07-16 08:14:35 +000043 ar.raw[59] = xread_char(archive_handle->src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000044 archive_handle->offset++;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000045 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000046 archive_handle->offset += 60;
Glenn L McGrath91e46462003-07-31 01:53:50 +000047
Glenn L McGrath95ebf612001-10-25 14:18:08 +000048 /* align the headers based on the header magic */
Denis Vlasenko666da5e2006-12-26 18:17:42 +000049 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
Denis Vlasenko13858992006-10-08 12:49:22 +000050 bb_error_msg_and_die("invalid ar header");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000051
Denis Vlasenko666da5e2006-12-26 18:17:42 +000052 /* FIXME: more thorough routine would be in order here */
53 /* (we have something like that in tar) */
54 /* but for now we are lax. This code works because */
55 /* on misformatted numbers bb_strtou returns all-ones */
56 typed->mode = err = bb_strtou(ar.formatted.mode, NULL, 8);
57 if (err == -1) bb_error_msg_and_die("invalid ar header");
58 typed->mtime = err = bb_strtou(ar.formatted.date, NULL, 10);
59 if (err == -1) bb_error_msg_and_die("invalid ar header");
60 typed->uid = err = bb_strtou(ar.formatted.uid, NULL, 10);
61 if (err == -1) bb_error_msg_and_die("invalid ar header");
62 typed->gid = err = bb_strtou(ar.formatted.gid, NULL, 10);
63 if (err == -1) bb_error_msg_and_die("invalid ar header");
64 typed->size = err = bb_strtou(ar.formatted.size, NULL, 10);
65 if (err == -1) bb_error_msg_and_die("invalid ar header");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000066
Glenn L McGrath95ebf612001-10-25 14:18:08 +000067 /* long filenames have '/' as the first character */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000068 if (ar.formatted.name[0] == '/') {
Denis Vlasenko97fd6d82007-03-19 20:59:20 +000069#if ENABLE_FEATURE_AR_LONG_FILENAMES
Denis Vlasenko650a0452007-03-14 22:08:53 +000070 unsigned long_offset;
71
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000072 if (ar.formatted.name[1] == '/') {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000073 /* If the second char is a '/' then this entries data section
74 * stores long filename for multiple entries, they are stored
75 * in static variable long_names for use in future entries */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000076 ar_long_name_size = typed->size;
77 ar_long_names = xmalloc(ar_long_name_size);
Rob Landley53437472006-07-16 08:14:35 +000078 xread(archive_handle->src_fd, ar_long_names, ar_long_name_size);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000079 archive_handle->offset += ar_long_name_size;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000080 /* This ar entries data section only contained filenames for other records
81 * they are stored in the static ar_long_names for future reference */
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000082 return get_header_ar(archive_handle); /* Return next header */
Denis Vlasenko650a0452007-03-14 22:08:53 +000083 }
84
85 if (ar.formatted.name[1] == ' ') {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000086 /* This is the index of symbols in the file for compilers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000087 data_skip(archive_handle);
Glenn L McGrath08ca7522004-01-04 11:06:34 +000088 archive_handle->offset += typed->size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000089 return get_header_ar(archive_handle); /* Return next header */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000090 }
Denis Vlasenko650a0452007-03-14 22:08:53 +000091
92 /* The number after the '/' indicates the offset in the ar data section
93 * (saved in variable long_name) that conatains the real filename */
94 long_offset = atoi(&ar.formatted.name[1]);
95 if (long_offset >= ar_long_name_size) {
96 bb_error_msg_and_die("can't resolve long filename");
97 }
98 typed->name = xstrdup(ar_long_names + long_offset);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000099#else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_error_msg_and_die("long filenames not supported");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000101#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000102 } else {
103 /* short filenames */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000104 typed->name = xstrndup(ar.formatted.name, 16);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000105 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000106
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000107 typed->name[strcspn(typed->name, " /")] = '\0';
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000108
Glenn L McGrath8e940982002-11-04 23:47:31 +0000109 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000110 archive_handle->action_header(typed);
111 if (archive_handle->sub_archive) {
Denis Vlasenko714701c2006-12-22 00:21:07 +0000112 while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000113 continue;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000114 } else {
115 archive_handle->action_data(archive_handle);
116 }
117 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000118 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000119 }
120
Glenn L McGrath91e46462003-07-31 01:53:50 +0000121 archive_handle->offset += typed->size;
122 /* Set the file pointer to the correct spot, we may have been reading a compressed file */
123 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000124
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000125 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000126}