blob: e6d712d8db28b3520061e3ac15ddcbebc864ba9a [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
Denys Vlasenko2bf66342009-09-20 01:28:27 +020010static unsigned read_num(const char *str, int base)
11{
12 /* This code works because
13 * on misformatted numbers bb_strtou returns all-ones */
14 int err = bb_strtou(str, NULL, base);
15 if (err == -1)
16 bb_error_msg_and_die("invalid ar header");
17 return err;
18}
19
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000021{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000022 file_header_t *typed = archive_handle->file_header;
Denys Vlasenko2bf66342009-09-20 01:28:27 +020023 unsigned size;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000024 union {
25 char raw[60];
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026 struct {
27 char name[16];
28 char date[12];
29 char uid[6];
30 char gid[6];
31 char mode[8];
32 char size[10];
33 char magic[2];
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000034 } formatted;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000035 } ar;
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000036#if ENABLE_FEATURE_AR_LONG_FILENAMES
Glenn L McGrath95ebf612001-10-25 14:18:08 +000037 static char *ar_long_names;
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000038 static unsigned ar_long_name_size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +000040
Rob Landleyd921b2e2006-08-03 15:41:12 +000041 /* dont use xread as we want to handle the error ourself */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000042 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
43 /* End Of File */
Denis Vlasenko079f8af2006-11-27 16:49:31 +000044 return EXIT_FAILURE;
Glenn L McGrath91e46462003-07-31 01:53:50 +000045 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000046
Glenn L McGrath958ac182004-04-09 06:59:05 +000047 /* ar header starts on an even byte (2 byte aligned)
48 * '\n' is used for padding
49 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000050 if (ar.raw[0] == '\n') {
51 /* fix up the header, we started reading 1 byte too early */
52 memmove(ar.raw, &ar.raw[1], 59);
Rob Landley53437472006-07-16 08:14:35 +000053 ar.raw[59] = xread_char(archive_handle->src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000054 archive_handle->offset++;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000055 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000056 archive_handle->offset += 60;
Glenn L McGrath91e46462003-07-31 01:53:50 +000057
Glenn L McGrath95ebf612001-10-25 14:18:08 +000058 /* align the headers based on the header magic */
Denis Vlasenko666da5e2006-12-26 18:17:42 +000059 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
Denis Vlasenko13858992006-10-08 12:49:22 +000060 bb_error_msg_and_die("invalid ar header");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000061
Denys Vlasenko2bf66342009-09-20 01:28:27 +020062 /* FIXME: more thorough routine would be in order here
63 * (we have something like that in tar)
64 * but for now we are lax. */
65 typed->size = size = read_num(ar.formatted.size, 10);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000066
Denys Vlasenko2bf66342009-09-20 01:28:27 +020067 /* special filenames have '/' as the first character */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000068 if (ar.formatted.name[0] == '/') {
Denis Vlasenko650a0452007-03-14 22:08:53 +000069 if (ar.formatted.name[1] == ' ') {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000070 /* This is the index of symbols in the file for compilers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000071 data_skip(archive_handle);
Denys Vlasenko2bf66342009-09-20 01:28:27 +020072 archive_handle->offset += size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000073 return get_header_ar(archive_handle); /* Return next header */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000074 }
Denys Vlasenko2bf66342009-09-20 01:28:27 +020075#if ENABLE_FEATURE_AR_LONG_FILENAMES
76 if (ar.formatted.name[1] == '/') {
77 /* If the second char is a '/' then this entries data section
78 * stores long filename for multiple entries, they are stored
79 * in static variable long_names for use in future entries
80 */
81 ar_long_name_size = size;
82 free(ar_long_names);
83 ar_long_names = xmalloc(size);
84 xread(archive_handle->src_fd, ar_long_names, size);
85 archive_handle->offset += size;
86 /* Return next header */
87 return get_header_ar(archive_handle);
88 }
89#else
90 bb_error_msg_and_die("long filenames not supported");
91#endif
92 }
93 /* Only size is always present, the rest may be missing in
94 * long filename pseudo file. Thus we decode the rest
95 * after dealing with long filename pseudo file.
96 */
97 typed->mode = read_num(ar.formatted.mode, 8);
98 typed->mtime = read_num(ar.formatted.date, 10);
99 typed->uid = read_num(ar.formatted.uid, 10);
100 typed->gid = read_num(ar.formatted.gid, 10);
101
102#if ENABLE_FEATURE_AR_LONG_FILENAMES
103 if (ar.formatted.name[0] == '/') {
104 unsigned long_offset;
Denis Vlasenko650a0452007-03-14 22:08:53 +0000105
106 /* The number after the '/' indicates the offset in the ar data section
Denys Vlasenko2bf66342009-09-20 01:28:27 +0200107 * (saved in ar_long_names) that conatains the real filename */
108 long_offset = read_num(&ar.formatted.name[1], 10);
Denis Vlasenko650a0452007-03-14 22:08:53 +0000109 if (long_offset >= ar_long_name_size) {
110 bb_error_msg_and_die("can't resolve long filename");
111 }
112 typed->name = xstrdup(ar_long_names + long_offset);
Denys Vlasenko2bf66342009-09-20 01:28:27 +0200113 } else
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000114#endif
Denys Vlasenko2bf66342009-09-20 01:28:27 +0200115 {
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000116 /* short filenames */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000117 typed->name = xstrndup(ar.formatted.name, 16);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000118 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000119
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000120 typed->name[strcspn(typed->name, " /")] = '\0';
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000121
Glenn L McGrath8e940982002-11-04 23:47:31 +0000122 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000123 archive_handle->action_header(typed);
Denis Vlasenko0381d422008-07-10 23:06:00 +0000124#if ENABLE_DPKG || ENABLE_DPKG_DEB
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000125 if (archive_handle->sub_archive) {
Denis Vlasenko714701c2006-12-22 00:21:07 +0000126 while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000127 continue;
Denis Vlasenko0381d422008-07-10 23:06:00 +0000128 } else
129#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000130 archive_handle->action_data(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000131 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000132 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000133 }
134
Glenn L McGrath91e46462003-07-31 01:53:50 +0000135 archive_handle->offset += typed->size;
136 /* Set the file pointer to the correct spot, we may have been reading a compressed file */
137 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000138
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000139 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000140}