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