1 | /* Copyright 2001 Glenn McGrath.
|
---|
2 | *
|
---|
3 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <stdlib.h>
|
---|
8 | #include <string.h>
|
---|
9 | #include <unistd.h>
|
---|
10 | #include "unarchive.h"
|
---|
11 | #include "libbb.h"
|
---|
12 |
|
---|
13 | char get_header_ar(archive_handle_t *archive_handle)
|
---|
14 | {
|
---|
15 | file_header_t *typed = archive_handle->file_header;
|
---|
16 | union {
|
---|
17 | char raw[60];
|
---|
18 | struct {
|
---|
19 | char name[16];
|
---|
20 | char date[12];
|
---|
21 | char uid[6];
|
---|
22 | char gid[6];
|
---|
23 | char mode[8];
|
---|
24 | char size[10];
|
---|
25 | char magic[2];
|
---|
26 | } formated;
|
---|
27 | } ar;
|
---|
28 | #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
|
---|
29 | static char *ar_long_names;
|
---|
30 | static unsigned int ar_long_name_size;
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | /* dont use bb_xread as we want to handle the error ourself */
|
---|
34 | if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
|
---|
35 | /* End Of File */
|
---|
36 | return(EXIT_FAILURE);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /* ar header starts on an even byte (2 byte aligned)
|
---|
40 | * '\n' is used for padding
|
---|
41 | */
|
---|
42 | if (ar.raw[0] == '\n') {
|
---|
43 | /* fix up the header, we started reading 1 byte too early */
|
---|
44 | memmove(ar.raw, &ar.raw[1], 59);
|
---|
45 | ar.raw[59] = bb_xread_char(archive_handle->src_fd);
|
---|
46 | archive_handle->offset++;
|
---|
47 | }
|
---|
48 | archive_handle->offset += 60;
|
---|
49 |
|
---|
50 | /* align the headers based on the header magic */
|
---|
51 | if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
|
---|
52 | bb_error_msg_and_die("Invalid ar header");
|
---|
53 | }
|
---|
54 |
|
---|
55 | typed->mode = strtol(ar.formated.mode, NULL, 8);
|
---|
56 | typed->mtime = atoi(ar.formated.date);
|
---|
57 | typed->uid = atoi(ar.formated.uid);
|
---|
58 | typed->gid = atoi(ar.formated.gid);
|
---|
59 | typed->size = atoi(ar.formated.size);
|
---|
60 |
|
---|
61 | /* long filenames have '/' as the first character */
|
---|
62 | if (ar.formated.name[0] == '/') {
|
---|
63 | #ifdef CONFIG_FEATURE_AR_LONG_FILENAMES
|
---|
64 | if (ar.formated.name[1] == '/') {
|
---|
65 | /* If the second char is a '/' then this entries data section
|
---|
66 | * stores long filename for multiple entries, they are stored
|
---|
67 | * in static variable long_names for use in future entries */
|
---|
68 | ar_long_name_size = typed->size;
|
---|
69 | ar_long_names = xmalloc(ar_long_name_size);
|
---|
70 | bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size);
|
---|
71 | archive_handle->offset += ar_long_name_size;
|
---|
72 | /* This ar entries data section only contained filenames for other records
|
---|
73 | * they are stored in the static ar_long_names for future reference */
|
---|
74 | return (get_header_ar(archive_handle)); /* Return next header */
|
---|
75 | } else if (ar.formated.name[1] == ' ') {
|
---|
76 | /* This is the index of symbols in the file for compilers */
|
---|
77 | data_skip(archive_handle);
|
---|
78 | archive_handle->offset += typed->size;
|
---|
79 | return (get_header_ar(archive_handle)); /* Return next header */
|
---|
80 | } else {
|
---|
81 | /* The number after the '/' indicates the offset in the ar data section
|
---|
82 | (saved in variable long_name) that conatains the real filename */
|
---|
83 | const unsigned int long_offset = atoi(&ar.formated.name[1]);
|
---|
84 | if (long_offset >= ar_long_name_size) {
|
---|
85 | bb_error_msg_and_die("Cant resolve long filename");
|
---|
86 | }
|
---|
87 | typed->name = bb_xstrdup(ar_long_names + long_offset);
|
---|
88 | }
|
---|
89 | #else
|
---|
90 | bb_error_msg_and_die("long filenames not supported");
|
---|
91 | #endif
|
---|
92 | } else {
|
---|
93 | /* short filenames */
|
---|
94 | typed->name = bb_xstrndup(ar.formated.name, 16);
|
---|
95 | }
|
---|
96 |
|
---|
97 | typed->name[strcspn(typed->name, " /")] = '\0';
|
---|
98 |
|
---|
99 | if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) {
|
---|
100 | archive_handle->action_header(typed);
|
---|
101 | if (archive_handle->sub_archive) {
|
---|
102 | while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS);
|
---|
103 | } else {
|
---|
104 | archive_handle->action_data(archive_handle);
|
---|
105 | }
|
---|
106 | } else {
|
---|
107 | data_skip(archive_handle);
|
---|
108 | }
|
---|
109 |
|
---|
110 | archive_handle->offset += typed->size;
|
---|
111 | /* Set the file pointer to the correct spot, we may have been reading a compressed file */
|
---|
112 | lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET);
|
---|
113 |
|
---|
114 | return(EXIT_SUCCESS);
|
---|
115 | }
|
---|