1/* vi: set sw=4 ts=4: */ 2/* Copyright 2001 Glenn McGrath. 3 * 4 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 5 */ 6 7#include "libbb.h" 8#include "unarchive.h" 9 10char get_header_ar(archive_handle_t *archive_handle) 11{ 12 int err; 13 file_header_t *typed = archive_handle->file_header; 14 union { 15 char raw[60]; 16 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]; 24 } formatted; 25 } ar; 26#if ENABLE_FEATURE_AR_LONG_FILENAMES 27 static char *ar_long_names; 28 static unsigned ar_long_name_size; 29#endif 30 31 /* dont use xread as we want to handle the error ourself */ 32 if (read(archive_handle->src_fd, ar.raw, 60) != 60) { 33 /* End Of File */ 34 return EXIT_FAILURE; 35 } 36 37 /* ar header starts on an even byte (2 byte aligned) 38 * '\n' is used for padding 39 */ 40 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); 43 ar.raw[59] = xread_char(archive_handle->src_fd); 44 archive_handle->offset++; 45 } 46 archive_handle->offset += 60; 47 48 /* align the headers based on the header magic */ 49 if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n') 50 bb_error_msg_and_die("invalid ar header"); 51 52 /* (we have something like that in tar) */ 53 /* but for now we are lax. This code works because */ 54 /* on misformatted numbers bb_strtou returns all-ones */ 55 typed->mode = err = bb_strtou(ar.formatted.mode, NULL, 8); 56 if (err == -1) bb_error_msg_and_die("invalid ar header"); 57 typed->mtime = err = bb_strtou(ar.formatted.date, NULL, 10); 58 if (err == -1) bb_error_msg_and_die("invalid ar header"); 59 typed->uid = err = bb_strtou(ar.formatted.uid, NULL, 10); 60 if (err == -1) bb_error_msg_and_die("invalid ar header"); 61 typed->gid = err = bb_strtou(ar.formatted.gid, NULL, 10); 62 if (err == -1) bb_error_msg_and_die("invalid ar header"); 63 typed->size = err = bb_strtou(ar.formatted.size, NULL, 10); 64 if (err == -1) bb_error_msg_and_die("invalid ar header"); 65 66 /* long filenames have '/' as the first character */ 67 if (ar.formatted.name[0] == '/') { 68#if ENABLE_FEATURE_AR_LONG_FILENAMES 69 unsigned long_offset; 70 71 if (ar.formatted.name[1] == '/') { 72 /* If the second char is a '/' then this entries data section 73 * stores long filename for multiple entries, they are stored 74 * in static variable long_names for use in future entries */ 75 ar_long_name_size = typed->size; 76 ar_long_names = xmalloc(ar_long_name_size); 77 xread(archive_handle->src_fd, ar_long_names, ar_long_name_size); 78 archive_handle->offset += ar_long_name_size; 79 /* This ar entries data section only contained filenames for other records 80 * they are stored in the static ar_long_names for future reference */ 81 return get_header_ar(archive_handle); /* Return next header */ 82 } 83 84 if (ar.formatted.name[1] == ' ') { 85 /* This is the index of symbols in the file for compilers */ 86 data_skip(archive_handle); 87 archive_handle->offset += typed->size; 88 return get_header_ar(archive_handle); /* Return next header */ 89 } 90 91 /* The number after the '/' indicates the offset in the ar data section 92 * (saved in variable long_name) that conatains the real filename */ 93 long_offset = atoi(&ar.formatted.name[1]); 94 if (long_offset >= ar_long_name_size) { 95 bb_error_msg_and_die("can't resolve long filename"); 96 } 97 typed->name = xstrdup(ar_long_names + long_offset); 98#else 99 bb_error_msg_and_die("long filenames not supported"); 100#endif 101 } else { 102 /* short filenames */ 103 typed->name = xstrndup(ar.formatted.name, 16); 104 } 105 106 typed->name[strcspn(typed->name, " /")] = '\0'; 107 108 if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { 109 archive_handle->action_header(typed); 110 if (archive_handle->sub_archive) { 111 while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS) 112 /* repeat */; 113 } else { 114 archive_handle->action_data(archive_handle); 115 } 116 } else { 117 data_skip(archive_handle); 118 } 119 120 archive_handle->offset += typed->size; 121 /* Set the file pointer to the correct spot, we may have been reading a compressed file */ 122 lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET); 123 124 return EXIT_SUCCESS; 125} 126