Merge from vendor branch FILE:
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_format_ar.c
1 /*-
2  * Copyright (c) 2007 Kai Wang
3  * Copyright (c) 2007 Tim Kientzle
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer
11  *    in this position and unchanged.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_ar.c,v 1.6 2007/05/29 01:00:19 kientzle Exp $");
30
31 #ifdef HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #endif
43
44 #include "archive.h"
45 #include "archive_entry.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
48
49 struct ar {
50         off_t    entry_bytes_remaining;
51         off_t    entry_offset;
52         off_t    entry_padding;
53         char    *strtab;
54         size_t   strtab_size;
55 };
56
57 /*
58  * Define structure of the "ar" header.
59  */
60 #define AR_name_offset 0
61 #define AR_name_size 16
62 #define AR_date_offset 16
63 #define AR_date_size 12
64 #define AR_uid_offset 28
65 #define AR_uid_size 6
66 #define AR_gid_offset 34
67 #define AR_gid_size 6
68 #define AR_mode_offset 40
69 #define AR_mode_size 8
70 #define AR_size_offset 48
71 #define AR_size_size 10
72 #define AR_fmag_offset 58
73 #define AR_fmag_size 2
74
75 #define isdigit(x)      (x) >= '0' && (x) <= '9'
76
77 static int      archive_read_format_ar_bid(struct archive_read *a);
78 static int      archive_read_format_ar_cleanup(struct archive_read *a);
79 static int      archive_read_format_ar_read_data(struct archive_read *a,
80                     const void **buff, size_t *size, off_t *offset);
81 static int      archive_read_format_ar_skip(struct archive_read *a);
82 static int      archive_read_format_ar_read_header(struct archive_read *a,
83                     struct archive_entry *e);
84 static uint64_t ar_atol8(const char *p, unsigned char_cnt);
85 static uint64_t ar_atol10(const char *p, unsigned char_cnt);
86 static int      ar_parse_gnu_filename_table(struct archive_read *, struct ar *,
87                     const void *, size_t);
88 static int      ar_parse_common_header(struct ar *ar, struct archive_entry *,
89                     const char *h);
90
91 int
92 archive_read_support_format_ar(struct archive *_a)
93 {
94         struct archive_read *a = (struct archive_read *)_a;
95         struct ar *ar;
96         int r;
97
98         ar = (struct ar *)malloc(sizeof(*ar));
99         if (ar == NULL) {
100                 archive_set_error(&a->archive, ENOMEM,
101                     "Can't allocate ar data");
102                 return (ARCHIVE_FATAL);
103         }
104         memset(ar, 0, sizeof(*ar));
105         ar->strtab = NULL;
106
107         r = __archive_read_register_format(a,
108             ar,
109             archive_read_format_ar_bid,
110             archive_read_format_ar_read_header,
111             archive_read_format_ar_read_data,
112             archive_read_format_ar_skip,
113             archive_read_format_ar_cleanup);
114
115         if (r != ARCHIVE_OK) {
116                 free(ar);
117                 return (r);
118         }
119         return (ARCHIVE_OK);
120 }
121
122 static int
123 archive_read_format_ar_cleanup(struct archive_read *a)
124 {
125         struct ar *ar;
126
127         ar = (struct ar *)(a->format->data);
128         if (ar->strtab)
129                 free(ar->strtab);
130         free(ar);
131         (a->format->data) = NULL;
132         return (ARCHIVE_OK);
133 }
134
135 static int
136 archive_read_format_ar_bid(struct archive_read *a)
137 {
138         struct ar *ar;
139         ssize_t bytes_read;
140         const void *h;
141
142         if (a->archive.archive_format != 0 &&
143             (a->archive.archive_format & ARCHIVE_FORMAT_BASE_MASK) !=
144             ARCHIVE_FORMAT_AR)
145                 return(0);
146
147         ar = (struct ar *)(a->format->data);
148
149         /*
150          * Verify the 8-byte file signature.
151          * TODO: Do we need to check more than this?
152          */
153         bytes_read = (a->decompressor->read_ahead)(a, &h, 8);
154         if (bytes_read < 8)
155                 return (-1);
156         if (strncmp((const char*)h, "!<arch>\n", 8) == 0) {
157                 return (64);
158         }
159         return (-1);
160 }
161
162 static int
163 archive_read_format_ar_read_header(struct archive_read *a,
164     struct archive_entry *entry)
165 {
166         char filename[AR_name_size + 1];
167         struct ar *ar;
168         uint64_t number; /* Used to hold parsed numbers before validation. */
169         ssize_t bytes_read;
170         size_t bsd_name_length, entry_size;
171         char *p;
172         const void *b;
173         const char *h;
174         int r;
175
176         ar = (struct ar*)(a->format->data);
177
178         if (a->archive.file_position == 0) {
179                 /*
180                  * We are now at the beginning of the archive,
181                  * so we need first consume the ar global header.
182                  */
183                 (a->decompressor->consume)(a, 8);
184                 /* Set a default format code for now. */
185                 a->archive.archive_format = ARCHIVE_FORMAT_AR;
186         }
187
188         /* Read the header for the next file entry. */
189         bytes_read = (a->decompressor->read_ahead)(a, &b, 60);
190         if (bytes_read < 60) {
191                 /* Broken header. */
192                 return (ARCHIVE_EOF);
193         }
194         (a->decompressor->consume)(a, 60);
195         h = (const char *)b;
196
197         /* Verify the magic signature on the file header. */
198         if (strncmp(h + AR_fmag_offset, "`\n", 2) != 0) {
199                 archive_set_error(&a->archive, EINVAL,
200                     "Consistency check failed");
201                 return (ARCHIVE_WARN);
202         }
203
204         /* Copy filename into work buffer. */
205         strncpy(filename, h + AR_name_offset, AR_name_size);
206         filename[AR_name_size] = '\0';
207
208         /*
209          * Guess the format variant based on the filename.
210          */
211         if (a->archive.archive_format == ARCHIVE_FORMAT_AR) {
212                 /* We don't already know the variant, so let's guess. */
213                 /*
214                  * Biggest clue is presence of '/': GNU starts special
215                  * filenames with '/', appends '/' as terminator to
216                  * non-special names, so anything with '/' should be
217                  * GNU except for BSD long filenames.
218                  */
219                 if (strncmp(filename, "#1/", 3) == 0)
220                         a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
221                 else if (strchr(filename, '/') != NULL)
222                         a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
223                 else if (strncmp(filename, "__.SYMDEF", 9) == 0)
224                         a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
225                 /*
226                  * XXX Do GNU/SVR4 'ar' programs ever omit trailing '/'
227                  * if name exactly fills 16-byte field?  If so, we
228                  * can't assume entries without '/' are BSD. XXX
229                  */
230         }
231
232         /* Update format name from the code. */
233         if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU)
234                 a->archive.archive_format_name = "ar (GNU/SVR4)";
235         else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD)
236                 a->archive.archive_format_name = "ar (BSD)";
237         else
238                 a->archive.archive_format_name = "ar";
239
240         /*
241          * Remove trailing spaces from the filename.  GNU and BSD
242          * variants both pad filename area out with spaces.
243          * This will only be wrong if GNU/SVR4 'ar' implementations
244          * omit trailing '/' for 16-char filenames and we have
245          * a 16-char filename that ends in ' '.
246          */
247         p = filename + AR_name_size - 1;
248         while (p >= filename && *p == ' ') {
249                 *p = '\0';
250                 p--;
251         }
252
253         /*
254          * Remove trailing slash unless first character is '/'.
255          * (BSD entries never end in '/', so this will only trim
256          * GNU-format entries.  GNU special entries start with '/'
257          * and are not terminated in '/', so we don't trim anything
258          * that starts with '/'.)
259          */
260         if (filename[0] != '/' && *p == '/')
261                 *p = '\0';
262
263         /*
264          * '//' is the GNU filename table.
265          * Later entries can refer to names in this table.
266          */
267         if (strcmp(filename, "//") == 0) {
268                 /* This must come before any call to _read_ahead. */
269                 ar_parse_common_header(ar, entry, h);
270                 archive_entry_copy_pathname(entry, filename);
271                 archive_entry_set_mode(entry,
272                     S_IFREG | (archive_entry_mode(entry) & 0777));
273                 /* Get the size of the filename table. */
274                 number = ar_atol10(h + AR_size_offset, AR_size_size);
275                 if (number > SIZE_MAX) {
276                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
277                             "Filename table too large");
278                         return (ARCHIVE_FATAL);
279                 }
280                 entry_size = (size_t)number;
281                 /* Read the filename table into memory. */
282                 bytes_read = (a->decompressor->read_ahead)(a, &b, entry_size);
283                 if (bytes_read <= 0)
284                         return (ARCHIVE_FATAL);
285                 if ((size_t)bytes_read < entry_size) {
286                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
287                             "Truncated input file");
288                         return (ARCHIVE_FATAL);
289                 }
290                 /*
291                  * Don't consume the contents, so the client will
292                  * also get a shot at reading it.
293                  */
294
295                 /* Parse the filename table. */
296                 return (ar_parse_gnu_filename_table(a, ar, b, entry_size));
297         }
298
299         /*
300          * GNU variant handles long filenames by storing /<number>
301          * to indicate a name stored in the filename table.
302          */
303         if (filename[0] == '/' && isdigit(filename[1])) {
304                 number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1);
305                 /*
306                  * If we can't look up the real name, warn and return
307                  * the entry with the wrong name.
308                  */
309                 if (ar->strtab == NULL || number > ar->strtab_size) {
310                         archive_set_error(&a->archive, EINVAL,
311                             "Can't find long filename for entry");
312                         archive_entry_copy_pathname(entry, filename);
313                         /* Parse the time, owner, mode, size fields. */
314                         ar_parse_common_header(ar, entry, h);
315                         return (ARCHIVE_WARN);
316                 }
317
318                 archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]);
319                 /* Parse the time, owner, mode, size fields. */
320                 return (ar_parse_common_header(ar, entry, h));
321         }
322
323         /*
324          * BSD handles long filenames by storing "#1/" followed by the
325          * length of filename as a decimal number, then prepends the
326          * the filename to the file contents.
327          */
328         if (strncmp(filename, "#1/", 3) == 0) {
329                 /* Parse the time, owner, mode, size fields. */
330                 /* This must occur before _read_ahead is called again. */
331                 ar_parse_common_header(ar, entry, h);
332
333                 /* Parse the size of the name, adjust the file size. */
334                 number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3);
335                 if ((off_t)number > ar->entry_bytes_remaining) {
336                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
337                             "Bad input file size");
338                         return (ARCHIVE_FATAL);
339                 }
340                 bsd_name_length = (size_t)number;
341                 ar->entry_bytes_remaining -= bsd_name_length;
342                 /* Adjust file size reported to client. */
343                 archive_entry_set_size(entry, ar->entry_bytes_remaining);
344
345                 /* Read the long name into memory. */
346                 bytes_read = (a->decompressor->read_ahead)(a, &b, bsd_name_length);
347                 if (bytes_read <= 0)
348                         return (ARCHIVE_FATAL);
349                 if ((size_t)bytes_read < bsd_name_length) {
350                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
351                             "Truncated input file");
352                         return (ARCHIVE_FATAL);
353                 }
354                 (a->decompressor->consume)(a, bsd_name_length);
355
356                 /* Store it in the entry. */
357                 p = (char *)malloc(bsd_name_length + 1);
358                 if (p == NULL) {
359                         archive_set_error(&a->archive, ENOMEM,
360                             "Can't allocate fname buffer");
361                         return (ARCHIVE_FATAL);
362                 }
363                 strncpy(p, b, bsd_name_length);
364                 p[bsd_name_length] = '\0';
365                 archive_entry_copy_pathname(entry, p);
366                 free(p);
367                 return (ARCHIVE_OK);
368         }
369
370         /*
371          * "/" is the SVR4/GNU archive symbol table.
372          */
373         if (strcmp(filename, "/") == 0) {
374                 archive_entry_copy_pathname(entry, "/");
375                 /* Parse the time, owner, mode, size fields. */
376                 r = ar_parse_common_header(ar, entry, h);
377                 /* Force the file type to a regular file. */
378                 archive_entry_set_mode(entry,
379                     S_IFREG | (archive_entry_mode(entry) & 0777));
380                 return (r);
381         }
382
383         /*
384          * "__.SYMDEF" is a BSD archive symbol table.
385          */
386         if (strcmp(filename, "__.SYMDEF") == 0) {
387                 archive_entry_copy_pathname(entry, filename);
388                 /* Parse the time, owner, mode, size fields. */
389                 return (ar_parse_common_header(ar, entry, h));
390         }
391
392         /*
393          * Otherwise, this is a standard entry.  The filename
394          * has already been trimmed as much as possible, based
395          * on our current knowledge of the format.
396          */
397         archive_entry_copy_pathname(entry, filename);
398         return (ar_parse_common_header(ar, entry, h));
399 }
400
401 static int
402 ar_parse_common_header(struct ar *ar, struct archive_entry *entry,
403     const char *h)
404 {
405         uint64_t n;
406
407         /* Copy remaining header */
408         archive_entry_set_mtime(entry,
409             (time_t)ar_atol10(h + AR_date_offset, AR_date_size), 0L);
410         archive_entry_set_uid(entry,
411             (uid_t)ar_atol10(h + AR_uid_offset, AR_uid_size));
412         archive_entry_set_gid(entry,
413             (gid_t)ar_atol10(h + AR_gid_offset, AR_gid_size));
414         archive_entry_set_mode(entry,
415             (mode_t)ar_atol8(h + AR_mode_offset, AR_mode_size));
416         n = ar_atol10(h + AR_size_offset, AR_size_size);
417
418         ar->entry_offset = 0;
419         ar->entry_padding = n % 2;
420         archive_entry_set_size(entry, n);
421         ar->entry_bytes_remaining = n;
422         return (ARCHIVE_OK);
423 }
424
425 static int
426 archive_read_format_ar_read_data(struct archive_read *a,
427     const void **buff, size_t *size, off_t *offset)
428 {
429         ssize_t bytes_read;
430         struct ar *ar;
431
432         ar = (struct ar *)(a->format->data);
433
434         if (ar->entry_bytes_remaining > 0) {
435                 bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
436                 if (bytes_read == 0) {
437                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
438                             "Truncated ar archive");
439                         return (ARCHIVE_FATAL);
440                 }
441                 if (bytes_read < 0)
442                         return (ARCHIVE_FATAL);
443                 if (bytes_read > ar->entry_bytes_remaining)
444                         bytes_read = (ssize_t)ar->entry_bytes_remaining;
445                 *size = bytes_read;
446                 *offset = ar->entry_offset;
447                 ar->entry_offset += bytes_read;
448                 ar->entry_bytes_remaining -= bytes_read;
449                 (a->decompressor->consume)(a, (size_t)bytes_read);
450                 return (ARCHIVE_OK);
451         } else {
452                 while (ar->entry_padding > 0) {
453                         bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
454                         if (bytes_read <= 0)
455                                 return (ARCHIVE_FATAL);
456                         if (bytes_read > ar->entry_padding)
457                                 bytes_read = (ssize_t)ar->entry_padding;
458                         (a->decompressor->consume)(a, (size_t)bytes_read);
459                         ar->entry_padding -= bytes_read;
460                 }
461                 *buff = NULL;
462                 *size = 0;
463                 *offset = ar->entry_offset;
464                 return (ARCHIVE_EOF);
465         }
466 }
467
468 static int
469 archive_read_format_ar_skip(struct archive_read *a)
470 {
471         off_t bytes_skipped;
472         struct ar* ar;
473         int r = ARCHIVE_OK;
474         const void *b;          /* Dummy variables */
475         size_t s;
476         off_t o;
477
478         ar = (struct ar *)(a->format->data);
479         if (a->decompressor->skip == NULL) {
480                 while (r == ARCHIVE_OK)
481                         r = archive_read_format_ar_read_data(a, &b, &s, &o);
482                 return (r);
483         }
484
485         bytes_skipped = (a->decompressor->skip)(a, ar->entry_bytes_remaining +
486             ar->entry_padding);
487         if (bytes_skipped < 0)
488                 return (ARCHIVE_FATAL);
489
490         ar->entry_bytes_remaining = 0;
491         ar->entry_padding = 0;
492
493         return (ARCHIVE_OK);
494 }
495
496 static int
497 ar_parse_gnu_filename_table(struct archive_read *a, struct ar *ar,
498     const void *h, size_t size)
499 {
500         char *p;
501
502         if (ar->strtab != NULL) {
503                 archive_set_error(&a->archive, EINVAL,
504                     "More than one string tables exist");
505                 return (ARCHIVE_WARN);
506         }
507
508         if (size == 0) {
509                 archive_set_error(&a->archive, EINVAL, "Invalid string table");
510                 return (ARCHIVE_WARN);
511         }
512
513         ar->strtab_size = size;
514         ar->strtab = malloc(size);
515         if (ar->strtab == NULL) {
516                 archive_set_error(&a->archive, ENOMEM,
517                     "Can't allocate string table buffer");
518                 return (ARCHIVE_FATAL);
519         }
520
521         (void)memcpy(ar->strtab, h, size);
522         for (p = ar->strtab; p < ar->strtab + size - 1; ++p) {
523                 if (*p == '/') {
524                         *p++ = '\0';
525                         if (*p != '\n')
526                                 goto bad_string_table;
527                         *p = '\0';
528                 }
529         }
530         /*
531          * Sanity check, last two chars must be `/\n' or '\n\n',
532          * depending on whether the string table is padded by a '\n'
533          * (string table produced by GNU ar always has a even size).
534          */
535         if (p != ar->strtab + size && *p != '\n')
536                 goto bad_string_table;
537
538         /* Enforce zero termination. */
539         ar->strtab[size - 1] = '\0';
540
541         return (ARCHIVE_OK);
542
543 bad_string_table:
544         archive_set_error(&a->archive, EINVAL,
545             "Invalid string table");
546         free(ar->strtab);
547         ar->strtab = NULL;
548         return (ARCHIVE_WARN);
549 }
550
551 static uint64_t
552 ar_atol8(const char *p, unsigned char_cnt)
553 {
554         uint64_t l, limit, last_digit_limit;
555         unsigned int digit, base;
556
557         base = 8;
558         limit = UINT64_MAX / base;
559         last_digit_limit = UINT64_MAX % base;
560
561         while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
562                 p++;
563
564         l = 0;
565         digit = *p - '0';
566         while (*p >= '0' && digit < base  && char_cnt-- > 0) {
567                 if (l>limit || (l == limit && digit > last_digit_limit)) {
568                         l = UINT64_MAX; /* Truncate on overflow. */
569                         break;
570                 }
571                 l = (l * base) + digit;
572                 digit = *++p - '0';
573         }
574         return (l);
575 }
576
577 static uint64_t
578 ar_atol10(const char *p, unsigned char_cnt)
579 {
580         uint64_t l, limit, last_digit_limit;
581         unsigned int base, digit;
582
583         base = 10;
584         limit = UINT64_MAX / base;
585         last_digit_limit = UINT64_MAX % base;
586
587         while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
588                 p++;
589         l = 0;
590         digit = *p - '0';
591         while (*p >= '0' && digit < base  && char_cnt-- > 0) {
592                 if (l > limit || (l == limit && digit > last_digit_limit)) {
593                         l = UINT64_MAX; /* Truncate on overflow. */
594                         break;
595                 }
596                 l = (l * base) + digit;
597                 digit = *++p - '0';
598         }
599         return (l);
600 }