"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame] | 2 | /* Copyright 2001 Glenn McGrath. |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 3 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 7 | #include "libbb.h" |
Denys Vlasenko | d184a72 | 2011-09-22 12:45:14 +0200 | [diff] [blame] | 8 | #include "bb_archive.h" |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 9 | #include "ar.h" |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 10 | |
Peter Korsgaard | 2a053a2 | 2013-09-10 11:52:35 +0200 | [diff] [blame] | 11 | /* WARNING: Clobbers str[len], so fields must be read in reverse order! */ |
| 12 | static unsigned read_num(char *str, int base, int len) |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 13 | { |
Peter Korsgaard | 2a053a2 | 2013-09-10 11:52:35 +0200 | [diff] [blame] | 14 | int err; |
| 15 | |
| 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 |
| 18 | * width is used. */ |
| 19 | str[len] = 0; |
| 20 | |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 21 | /* This code works because |
| 22 | * on misformatted numbers bb_strtou returns all-ones */ |
Peter Korsgaard | 2a053a2 | 2013-09-10 11:52:35 +0200 | [diff] [blame] | 23 | err = bb_strtou(str, NULL, base); |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 24 | if (err == -1) |
| 25 | bb_error_msg_and_die("invalid ar header"); |
| 26 | return err; |
| 27 | } |
| 28 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 29 | char FAST_FUNC get_header_ar(archive_handle_t *archive_handle) |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 30 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 31 | file_header_t *typed = archive_handle->file_header; |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 32 | unsigned size; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 33 | union { |
| 34 | char raw[60]; |
Alexander Shishkin | 535584c | 2010-03-15 15:38:09 +0100 | [diff] [blame] | 35 | struct ar_header formatted; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 36 | } ar; |
Denis Vlasenko | 04c99eb | 2007-04-07 00:44:31 +0000 | [diff] [blame] | 37 | #if ENABLE_FEATURE_AR_LONG_FILENAMES |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 38 | static char *ar_long_names; |
Denis Vlasenko | 04c99eb | 2007-04-07 00:44:31 +0000 | [diff] [blame] | 39 | static unsigned ar_long_name_size; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 40 | #endif |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 41 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 42 | /* dont use xread as we want to handle the error ourself */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 43 | if (read(archive_handle->src_fd, ar.raw, 60) != 60) { |
| 44 | /* End Of File */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 45 | return EXIT_FAILURE; |
Glenn L McGrath | 91e4646 | 2003-07-31 01:53:50 +0000 | [diff] [blame] | 46 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 47 | |
Glenn L McGrath | 958ac18 | 2004-04-09 06:59:05 +0000 | [diff] [blame] | 48 | /* ar header starts on an even byte (2 byte aligned) |
| 49 | * '\n' is used for padding |
| 50 | */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 51 | if (ar.raw[0] == '\n') { |
| 52 | /* fix up the header, we started reading 1 byte too early */ |
| 53 | memmove(ar.raw, &ar.raw[1], 59); |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 54 | ar.raw[59] = xread_char(archive_handle->src_fd); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 55 | archive_handle->offset++; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 56 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 57 | archive_handle->offset += 60; |
Glenn L McGrath | 91e4646 | 2003-07-31 01:53:50 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 59 | if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n') |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 60 | bb_error_msg_and_die("invalid ar header"); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 61 | |
Denys Vlasenko | a613aa1 | 2013-09-10 16:27:08 +0200 | [diff] [blame] | 62 | /* |
| 63 | * Note that the fields MUST be read in reverse order as |
| 64 | * read_num() clobbers the next byte after the field! |
| 65 | * Order is: name, date, uid, gid, mode, size, magic. |
| 66 | */ |
Peter Korsgaard | 2a053a2 | 2013-09-10 11:52:35 +0200 | [diff] [blame] | 67 | typed->size = size = read_num(ar.formatted.size, 10, |
| 68 | sizeof(ar.formatted.size)); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 69 | |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 70 | /* special filenames have '/' as the first character */ |
"Robert P. J. Day" | eea5618 | 2006-07-20 19:02:24 +0000 | [diff] [blame] | 71 | if (ar.formatted.name[0] == '/') { |
Denis Vlasenko | 650a045 | 2007-03-14 22:08:53 +0000 | [diff] [blame] | 72 | if (ar.formatted.name[1] == ' ') { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 73 | /* This is the index of symbols in the file for compilers */ |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 74 | data_skip(archive_handle); |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 75 | archive_handle->offset += size; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 76 | return get_header_ar(archive_handle); /* Return next header */ |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 77 | } |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 78 | #if ENABLE_FEATURE_AR_LONG_FILENAMES |
| 79 | if (ar.formatted.name[1] == '/') { |
| 80 | /* If the second char is a '/' then this entries data section |
| 81 | * stores long filename for multiple entries, they are stored |
| 82 | * in static variable long_names for use in future entries |
| 83 | */ |
| 84 | ar_long_name_size = size; |
| 85 | free(ar_long_names); |
| 86 | ar_long_names = xmalloc(size); |
| 87 | xread(archive_handle->src_fd, ar_long_names, size); |
| 88 | archive_handle->offset += size; |
| 89 | /* Return next header */ |
| 90 | return get_header_ar(archive_handle); |
| 91 | } |
| 92 | #else |
| 93 | bb_error_msg_and_die("long filenames not supported"); |
| 94 | #endif |
| 95 | } |
| 96 | /* Only size is always present, the rest may be missing in |
| 97 | * long filename pseudo file. Thus we decode the rest |
| 98 | * after dealing with long filename pseudo file. |
| 99 | */ |
Peter Korsgaard | 2a053a2 | 2013-09-10 11:52:35 +0200 | [diff] [blame] | 100 | typed->mode = read_num(ar.formatted.mode, 8, sizeof(ar.formatted.mode)); |
| 101 | typed->gid = read_num(ar.formatted.gid, 10, sizeof(ar.formatted.gid)); |
| 102 | typed->uid = read_num(ar.formatted.uid, 10, sizeof(ar.formatted.uid)); |
| 103 | typed->mtime = read_num(ar.formatted.date, 10, sizeof(ar.formatted.date)); |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 104 | |
| 105 | #if ENABLE_FEATURE_AR_LONG_FILENAMES |
| 106 | if (ar.formatted.name[0] == '/') { |
| 107 | unsigned long_offset; |
Denis Vlasenko | 650a045 | 2007-03-14 22:08:53 +0000 | [diff] [blame] | 108 | |
| 109 | /* The number after the '/' indicates the offset in the ar data section |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 110 | * (saved in ar_long_names) that conatains the real filename */ |
Peter Korsgaard | 2a053a2 | 2013-09-10 11:52:35 +0200 | [diff] [blame] | 111 | long_offset = read_num(&ar.formatted.name[1], 10, |
| 112 | sizeof(ar.formatted.name) - 1); |
Denis Vlasenko | 650a045 | 2007-03-14 22:08:53 +0000 | [diff] [blame] | 113 | if (long_offset >= ar_long_name_size) { |
| 114 | bb_error_msg_and_die("can't resolve long filename"); |
| 115 | } |
| 116 | typed->name = xstrdup(ar_long_names + long_offset); |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 117 | } else |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 118 | #endif |
Denys Vlasenko | 2bf6634 | 2009-09-20 01:28:27 +0200 | [diff] [blame] | 119 | { |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 120 | /* short filenames */ |
Denis Vlasenko | d677250 | 2006-11-24 17:21:44 +0000 | [diff] [blame] | 121 | typed->name = xstrndup(ar.formatted.name, 16); |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 122 | } |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 123 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 124 | typed->name[strcspn(typed->name, " /")] = '\0'; |
Glenn L McGrath | 95ebf61 | 2001-10-25 14:18:08 +0000 | [diff] [blame] | 125 | |
Glenn L McGrath | 8e94098 | 2002-11-04 23:47:31 +0000 | [diff] [blame] | 126 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 127 | archive_handle->action_header(typed); |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 128 | #if ENABLE_DPKG || ENABLE_DPKG_DEB |
Denys Vlasenko | aa4977d | 2010-01-06 10:53:17 +0100 | [diff] [blame] | 129 | if (archive_handle->dpkg__sub_archive) { |
| 130 | while (archive_handle->dpkg__action_data_subarchive(archive_handle->dpkg__sub_archive) == EXIT_SUCCESS) |
Denis Vlasenko | a60936d | 2008-06-28 05:04:09 +0000 | [diff] [blame] | 131 | continue; |
Denis Vlasenko | 0381d42 | 2008-07-10 23:06:00 +0000 | [diff] [blame] | 132 | } else |
| 133 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 134 | archive_handle->action_data(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 135 | } else { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 136 | data_skip(archive_handle); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Glenn L McGrath | 91e4646 | 2003-07-31 01:53:50 +0000 | [diff] [blame] | 139 | archive_handle->offset += typed->size; |
| 140 | /* Set the file pointer to the correct spot, we may have been reading a compressed file */ |
| 141 | lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 142 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 143 | return EXIT_SUCCESS; |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 144 | } |