blob: df603b11141f3314d5206eab71f5d604a24963d0 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath95ebf612001-10-25 14:18:08 +00005 */
6
Glenn L McGrath95ebf612001-10-25 14:18:08 +00007#include "libbb.h"
Denys Vlasenko833d4e72010-11-03 02:38:31 +01008#include "archive.h"
Alexander Shishkin535584c2010-03-15 15:38:09 +01009#include "ar.h"
Glenn L McGrath95ebf612001-10-25 14:18:08 +000010
Denys Vlasenko2bf66342009-09-20 01:28:27 +020011static unsigned read_num(const char *str, int base)
12{
13 /* This code works because
14 * on misformatted numbers bb_strtou returns all-ones */
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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000021char FAST_FUNC get_header_ar(archive_handle_t *archive_handle)
Glenn L McGrath95ebf612001-10-25 14:18:08 +000022{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000023 file_header_t *typed = archive_handle->file_header;
Denys Vlasenko2bf66342009-09-20 01:28:27 +020024 unsigned size;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000025 union {
26 char raw[60];
Alexander Shishkin535584c2010-03-15 15:38:09 +010027 struct ar_header formatted;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000028 } ar;
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000029#if ENABLE_FEATURE_AR_LONG_FILENAMES
Glenn L McGrath95ebf612001-10-25 14:18:08 +000030 static char *ar_long_names;
Denis Vlasenko04c99eb2007-04-07 00:44:31 +000031 static unsigned ar_long_name_size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000032#endif
Glenn L McGrath95ebf612001-10-25 14:18:08 +000033
Rob Landleyd921b2e2006-08-03 15:41:12 +000034 /* dont use xread as we want to handle the error ourself */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000035 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
36 /* End Of File */
Denis Vlasenko079f8af2006-11-27 16:49:31 +000037 return EXIT_FAILURE;
Glenn L McGrath91e46462003-07-31 01:53:50 +000038 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039
Glenn L McGrath958ac182004-04-09 06:59:05 +000040 /* ar header starts on an even byte (2 byte aligned)
41 * '\n' is used for padding
42 */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000043 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);
Rob Landley53437472006-07-16 08:14:35 +000046 ar.raw[59] = xread_char(archive_handle->src_fd);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000047 archive_handle->offset++;
Glenn L McGrath95ebf612001-10-25 14:18:08 +000048 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000049 archive_handle->offset += 60;
Glenn L McGrath91e46462003-07-31 01:53:50 +000050
Denis Vlasenko666da5e2006-12-26 18:17:42 +000051 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n')
Denis Vlasenko13858992006-10-08 12:49:22 +000052 bb_error_msg_and_die("invalid ar header");
Glenn L McGrath95ebf612001-10-25 14:18:08 +000053
Denys Vlasenko2bf66342009-09-20 01:28:27 +020054 /* FIXME: more thorough routine would be in order here
55 * (we have something like that in tar)
56 * but for now we are lax. */
Alexander Shishkin535584c2010-03-15 15:38:09 +010057 ar.formatted.magic[0] = '\0'; /* else 4G-2 file will have size="4294967294`\n..." */
Denys Vlasenko2bf66342009-09-20 01:28:27 +020058 typed->size = size = read_num(ar.formatted.size, 10);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000059
Denys Vlasenko2bf66342009-09-20 01:28:27 +020060 /* special filenames have '/' as the first character */
"Robert P. J. Day"eea56182006-07-20 19:02:24 +000061 if (ar.formatted.name[0] == '/') {
Denis Vlasenko650a0452007-03-14 22:08:53 +000062 if (ar.formatted.name[1] == ' ') {
Glenn L McGrath95ebf612001-10-25 14:18:08 +000063 /* This is the index of symbols in the file for compilers */
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000064 data_skip(archive_handle);
Denys Vlasenko2bf66342009-09-20 01:28:27 +020065 archive_handle->offset += size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000066 return get_header_ar(archive_handle); /* Return next header */
Glenn L McGrath95ebf612001-10-25 14:18:08 +000067 }
Denys Vlasenko2bf66342009-09-20 01:28:27 +020068#if ENABLE_FEATURE_AR_LONG_FILENAMES
69 if (ar.formatted.name[1] == '/') {
70 /* If the second char is a '/' then this entries data section
71 * stores long filename for multiple entries, they are stored
72 * in static variable long_names for use in future entries
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 /* Return next header */
80 return get_header_ar(archive_handle);
81 }
82#else
83 bb_error_msg_and_die("long filenames not supported");
84#endif
85 }
86 /* Only size is always present, the rest may be missing in
87 * long filename pseudo file. Thus we decode the rest
88 * after dealing with long filename pseudo file.
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;
Denis Vlasenko650a0452007-03-14 22:08:53 +000098
99 /* The number after the '/' indicates the offset in the ar data section
Denys Vlasenko2bf66342009-09-20 01:28:27 +0200100 * (saved in ar_long_names) that conatains the real filename */
101 long_offset = read_num(&ar.formatted.name[1], 10);
Denis Vlasenko650a0452007-03-14 22:08:53 +0000102 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);
Denys Vlasenko2bf66342009-09-20 01:28:27 +0200106 } else
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000107#endif
Denys Vlasenko2bf66342009-09-20 01:28:27 +0200108 {
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000109 /* short filenames */
Denis Vlasenkod6772502006-11-24 17:21:44 +0000110 typed->name = xstrndup(ar.formatted.name, 16);
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000111 }
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000112
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000113 typed->name[strcspn(typed->name, " /")] = '\0';
Glenn L McGrath95ebf612001-10-25 14:18:08 +0000114
Glenn L McGrath8e940982002-11-04 23:47:31 +0000115 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000116 archive_handle->action_header(typed);
Denis Vlasenko0381d422008-07-10 23:06:00 +0000117#if ENABLE_DPKG || ENABLE_DPKG_DEB
Denys Vlasenkoaa4977d2010-01-06 10:53:17 +0100118 if (archive_handle->dpkg__sub_archive) {
119 while (archive_handle->dpkg__action_data_subarchive(archive_handle->dpkg__sub_archive) == EXIT_SUCCESS)
Denis Vlasenkoa60936d2008-06-28 05:04:09 +0000120 continue;
Denis Vlasenko0381d422008-07-10 23:06:00 +0000121 } else
122#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000123 archive_handle->action_data(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000124 } else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000125 data_skip(archive_handle);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000126 }
127
Glenn L McGrath91e46462003-07-31 01:53:50 +0000128 archive_handle->offset += typed->size;
129 /* Set the file pointer to the correct spot, we may have been reading a compressed file */
130 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000131
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000132 return EXIT_SUCCESS;
Eric Andersen2276d832002-07-11 11:11:56 +0000133}