Import libarchive and bsdtar version 2.0.25
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_read_support_format_tar.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_tar.c,v 1.49 2007/03/03 07:37:36 kientzle Exp $");
28
29 #ifdef HAVE_SYS_STAT_H
30 #include <sys/stat.h>
31 #endif
32 #ifdef MAJOR_IN_MKDEV
33 #include <sys/mkdev.h>
34 #else
35 #ifdef MAJOR_IN_SYSMACROS
36 #include <sys/sysmacros.h>
37 #endif
38 #endif
39 #ifdef HAVE_ERRNO_H
40 #include <errno.h>
41 #endif
42 #include <stddef.h>
43 /* #include <stdint.h> */ /* See archive_platform.h */
44 #ifdef HAVE_STDLIB_H
45 #include <stdlib.h>
46 #endif
47 #ifdef HAVE_STRING_H
48 #include <string.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53
54 /* Obtain suitable wide-character manipulation functions. */
55 #ifdef HAVE_WCHAR_H
56 #include <wchar.h>
57 #else
58 /* Good enough for equality testing, which is all we need. */
59 static int wcscmp(const wchar_t *s1, const wchar_t *s2)
60 {
61         int diff = *s1 - *s2;
62         while (*s1 && diff == 0)
63                 diff = (int)*++s1 - (int)*++s2;
64         return diff;
65 }
66 /* Good enough for equality testing, which is all we need. */
67 static int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
68 {
69         int diff = *s1 - *s2;
70         while (*s1 && diff == 0 && n-- > 0)
71                 diff = (int)*++s1 - (int)*++s2;
72         return diff;
73 }
74 static size_t wcslen(const wchar_t *s)
75 {
76         const wchar_t *p = s;
77         while (*p)
78                 p++;
79         return p - s;
80 }
81 #endif
82
83 #include "archive.h"
84 #include "archive_entry.h"
85 #include "archive_private.h"
86 #include "archive_read_private.h"
87
88 /*
89  * Layout of POSIX 'ustar' tar header.
90  */
91 struct archive_entry_header_ustar {
92         char    name[100];
93         char    mode[8];
94         char    uid[8];
95         char    gid[8];
96         char    size[12];
97         char    mtime[12];
98         char    checksum[8];
99         char    typeflag[1];
100         char    linkname[100];  /* "old format" header ends here */
101         char    magic[6];       /* For POSIX: "ustar\0" */
102         char    version[2];     /* For POSIX: "00" */
103         char    uname[32];
104         char    gname[32];
105         char    rdevmajor[8];
106         char    rdevminor[8];
107         char    prefix[155];
108 };
109
110 /*
111  * Structure of GNU tar header
112  */
113 struct gnu_sparse {
114         char    offset[12];
115         char    numbytes[12];
116 };
117
118 struct archive_entry_header_gnutar {
119         char    name[100];
120         char    mode[8];
121         char    uid[8];
122         char    gid[8];
123         char    size[12];
124         char    mtime[12];
125         char    checksum[8];
126         char    typeflag[1];
127         char    linkname[100];
128         char    magic[8];  /* "ustar  \0" (note blank/blank/null at end) */
129         char    uname[32];
130         char    gname[32];
131         char    rdevmajor[8];
132         char    rdevminor[8];
133         char    atime[12];
134         char    ctime[12];
135         char    offset[12];
136         char    longnames[4];
137         char    unused[1];
138         struct gnu_sparse sparse[4];
139         char    isextended[1];
140         char    realsize[12];
141         /*
142          * GNU doesn't use POSIX 'prefix' field; they use the 'L' (longname)
143          * entry instead.
144          */
145 };
146
147 /*
148  * Data specific to this format.
149  */
150 struct sparse_block {
151         struct sparse_block     *next;
152         off_t   offset;
153         off_t   remaining;
154 };
155
156 struct tar {
157         struct archive_string    acl_text;
158         struct archive_string    entry_name;
159         struct archive_string    entry_linkname;
160         struct archive_string    entry_uname;
161         struct archive_string    entry_gname;
162         struct archive_string    longlink;
163         struct archive_string    longname;
164         struct archive_string    pax_header;
165         struct archive_string    pax_global;
166         wchar_t                 *pax_entry;
167         size_t                   pax_entry_length;
168         int                      header_recursion_depth;
169         off_t                    entry_bytes_remaining;
170         off_t                    entry_offset;
171         off_t                    entry_padding;
172         struct sparse_block     *sparse_list;
173 };
174
175 static size_t   UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n);
176 static int      archive_block_is_null(const unsigned char *p);
177 static char     *base64_decode(const wchar_t *, size_t, size_t *);
178 static int      gnu_read_sparse_data(struct archive_read *, struct tar *,
179                     const struct archive_entry_header_gnutar *header);
180 static void     gnu_parse_sparse_data(struct archive_read *, struct tar *,
181                     const struct gnu_sparse *sparse, int length);
182 static int      header_Solaris_ACL(struct archive_read *,  struct tar *,
183                     struct archive_entry *, struct stat *, const void *);
184 static int      header_common(struct archive_read *,  struct tar *,
185                     struct archive_entry *, struct stat *, const void *);
186 static int      header_old_tar(struct archive_read *, struct tar *,
187                     struct archive_entry *, struct stat *, const void *);
188 static int      header_pax_extensions(struct archive_read *, struct tar *,
189                     struct archive_entry *, struct stat *, const void *);
190 static int      header_pax_global(struct archive_read *, struct tar *,
191                     struct archive_entry *, struct stat *, const void *h);
192 static int      header_longlink(struct archive_read *, struct tar *,
193                     struct archive_entry *, struct stat *, const void *h);
194 static int      header_longname(struct archive_read *, struct tar *,
195                     struct archive_entry *, struct stat *, const void *h);
196 static int      header_volume(struct archive_read *, struct tar *,
197                     struct archive_entry *, struct stat *, const void *h);
198 static int      header_ustar(struct archive_read *, struct tar *,
199                     struct archive_entry *, struct stat *, const void *h);
200 static int      header_gnutar(struct archive_read *, struct tar *,
201                     struct archive_entry *, struct stat *, const void *h);
202 static int      archive_read_format_tar_bid(struct archive_read *);
203 static int      archive_read_format_tar_cleanup(struct archive_read *);
204 static int      archive_read_format_tar_read_data(struct archive_read *a,
205                     const void **buff, size_t *size, off_t *offset);
206 static int      archive_read_format_tar_skip(struct archive_read *a);
207 static int      archive_read_format_tar_read_header(struct archive_read *,
208                     struct archive_entry *);
209 static int      checksum(struct archive_read *, const void *);
210 static int      pax_attribute(struct archive_entry *, struct stat *,
211                     wchar_t *key, wchar_t *value);
212 static int      pax_header(struct archive_read *, struct tar *,
213                     struct archive_entry *, struct stat *, char *attr);
214 static void     pax_time(const wchar_t *, int64_t *sec, long *nanos);
215 static int      read_body_to_string(struct archive_read *, struct tar *,
216                     struct archive_string *, const void *h);
217 static int64_t  tar_atol(const char *, unsigned);
218 static int64_t  tar_atol10(const wchar_t *, unsigned);
219 static int64_t  tar_atol256(const char *, unsigned);
220 static int64_t  tar_atol8(const char *, unsigned);
221 static int      tar_read_header(struct archive_read *, struct tar *,
222                     struct archive_entry *, struct stat *);
223 static int      tohex(int c);
224 static char     *url_decode(const char *);
225 static int      utf8_decode(wchar_t *, const char *, size_t length);
226 static char     *wide_to_narrow(const wchar_t *wval);
227
228 /*
229  * ANSI C99 defines constants for these, but not everyone supports
230  * those constants, so I define a couple of static variables here and
231  * compute the values.  These calculations should be portable to any
232  * 2s-complement architecture.
233  */
234 #ifdef UINT64_MAX
235 static const uint64_t max_uint64 = UINT64_MAX;
236 #else
237 static const uint64_t max_uint64 = ~(uint64_t)0;
238 #endif
239 #ifdef INT64_MAX
240 static const int64_t max_int64 = INT64_MAX;
241 #else
242 static const int64_t max_int64 = (int64_t)((~(uint64_t)0) >> 1);
243 #endif
244 #ifdef INT64_MIN
245 static const int64_t min_int64 = INT64_MIN;
246 #else
247 static const int64_t min_int64 = (int64_t)(~((~(uint64_t)0) >> 1));
248 #endif
249
250 int
251 archive_read_support_format_gnutar(struct archive *a)
252 {
253         return (archive_read_support_format_tar(a));
254 }
255
256
257 int
258 archive_read_support_format_tar(struct archive *_a)
259 {
260         struct archive_read *a = (struct archive_read *)_a;
261         struct tar *tar;
262         int r;
263
264         tar = (struct tar *)malloc(sizeof(*tar));
265         if (tar == NULL) {
266                 archive_set_error(&a->archive, ENOMEM,
267                     "Can't allocate tar data");
268                 return (ARCHIVE_FATAL);
269         }
270         memset(tar, 0, sizeof(*tar));
271
272         r = __archive_read_register_format(a, tar,
273             archive_read_format_tar_bid,
274             archive_read_format_tar_read_header,
275             archive_read_format_tar_read_data,
276             archive_read_format_tar_skip,
277             archive_read_format_tar_cleanup);
278
279         if (r != ARCHIVE_OK)
280                 free(tar);
281         return (ARCHIVE_OK);
282 }
283
284 static int
285 archive_read_format_tar_cleanup(struct archive_read *a)
286 {
287         struct tar *tar;
288
289         tar = (struct tar *)*(a->pformat_data);
290         archive_string_free(&tar->acl_text);
291         archive_string_free(&tar->entry_name);
292         archive_string_free(&tar->entry_linkname);
293         archive_string_free(&tar->entry_uname);
294         archive_string_free(&tar->entry_gname);
295         archive_string_free(&tar->pax_global);
296         archive_string_free(&tar->pax_header);
297         if (tar->pax_entry != NULL)
298                 free(tar->pax_entry);
299         free(tar);
300         *(a->pformat_data) = NULL;
301         return (ARCHIVE_OK);
302 }
303
304
305 static int
306 archive_read_format_tar_bid(struct archive_read *a)
307 {
308         int bid;
309         ssize_t bytes_read;
310         const void *h;
311         const struct archive_entry_header_ustar *header;
312
313         /*
314          * If we're already reading a non-tar file, don't
315          * bother to bid.
316          */
317         if (a->archive.archive_format != 0 &&
318             (a->archive.archive_format & ARCHIVE_FORMAT_BASE_MASK) !=
319             ARCHIVE_FORMAT_TAR)
320                 return (0);
321         bid = 0;
322
323         /*
324          * If we're already reading a tar format, start the bid at 1 as
325          * a failsafe.
326          */
327         if ((a->archive.archive_format & ARCHIVE_FORMAT_BASE_MASK) ==
328             ARCHIVE_FORMAT_TAR)
329                 bid++;
330
331         /* Now let's look at the actual header and see if it matches. */
332         if (a->compression_read_ahead != NULL)
333                 bytes_read = (a->compression_read_ahead)(a, &h, 512);
334         else
335                 bytes_read = 0; /* Empty file. */
336         if (bytes_read < 0)
337                 return (ARCHIVE_FATAL);
338         if (bytes_read == 0  &&  bid > 0) {
339                 /* An archive without a proper end-of-archive marker. */
340                 /* Hold our nose and bid 1 anyway. */
341                 return (1);
342         }
343         if (bytes_read < 512) {
344                 /* If it's a new archive, then just return a zero bid. */
345                 if (bid == 0)
346                         return (0);
347                 /*
348                  * If we already know this is a tar archive,
349                  * then we have a problem.
350                  */
351                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
352                     "Truncated tar archive");
353                 return (ARCHIVE_FATAL);
354         }
355
356         /* If it's an end-of-archive mark, we can handle it. */
357         if ((*(const char *)h) == 0 && archive_block_is_null((const unsigned char *)h)) {
358                 /* If it's a known tar file, end-of-archive is definite. */
359                 if ((a->archive.archive_format & ARCHIVE_FORMAT_BASE_MASK) ==
360                     ARCHIVE_FORMAT_TAR)
361                         return (512);
362                 /* Empty archive? */
363                 return (1);
364         }
365
366         /* If it's not an end-of-archive mark, it must have a valid checksum.*/
367         if (!checksum(a, h))
368                 return (0);
369         bid += 48;  /* Checksum is usually 6 octal digits. */
370
371         header = (const struct archive_entry_header_ustar *)h;
372
373         /* Recognize POSIX formats. */
374         if ((memcmp(header->magic, "ustar\0", 6) == 0)
375             &&(memcmp(header->version, "00", 2)==0))
376                 bid += 56;
377
378         /* Recognize GNU tar format. */
379         if ((memcmp(header->magic, "ustar ", 6) == 0)
380             &&(memcmp(header->version, " \0", 2)==0))
381                 bid += 56;
382
383         /* Type flag must be null, digit or A-Z, a-z. */
384         if (header->typeflag[0] != 0 &&
385             !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
386             !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
387             !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
388                 return (0);
389         bid += 2;  /* 6 bits of variation in an 8-bit field leaves 2 bits. */
390
391         /* Sanity check: Look at first byte of mode field. */
392         switch (255 & (unsigned)header->mode[0]) {
393         case 0: case 255:
394                 /* Base-256 value: No further verification possible! */
395                 break;
396         case ' ': /* Not recommended, but not illegal, either. */
397                 break;
398         case '0': case '1': case '2': case '3':
399         case '4': case '5': case '6': case '7':
400                 /* Octal Value. */
401                 /* TODO: Check format of remainder of this field. */
402                 break;
403         default:
404                 /* Not a valid mode; bail out here. */
405                 return (0);
406         }
407         /* TODO: Sanity test uid/gid/size/mtime/rdevmajor/rdevminor fields. */
408
409         return (bid);
410 }
411
412 /*
413  * The function invoked by archive_read_header().  This
414  * just sets up a few things and then calls the internal
415  * tar_read_header() function below.
416  */
417 static int
418 archive_read_format_tar_read_header(struct archive_read *a,
419     struct archive_entry *entry)
420 {
421         /*
422          * When converting tar archives to cpio archives, it is
423          * essential that each distinct file have a distinct inode
424          * number.  To simplify this, we keep a static count here to
425          * assign fake dev/inode numbers to each tar entry.  Note that
426          * pax format archives may overwrite this with something more
427          * useful.
428          *
429          * Ideally, we would track every file read from the archive so
430          * that we could assign the same dev/ino pair to hardlinks,
431          * but the memory required to store a complete lookup table is
432          * probably not worthwhile just to support the relatively
433          * obscure tar->cpio conversion case.
434          */
435         static int default_inode;
436         static int default_dev;
437         struct stat st;
438         struct tar *tar;
439         const char *p;
440         int r;
441         size_t l;
442
443         memset(&st, 0, sizeof(st));
444         /* Assign default device/inode values. */
445         st.st_dev = 1 + default_dev; /* Don't use zero. */
446         st.st_ino = ++default_inode; /* Don't use zero. */
447         /* Limit generated st_ino number to 16 bits. */
448         if (default_inode >= 0xffff) {
449                 ++default_dev;
450                 default_inode = 0;
451         }
452
453         tar = (struct tar *)*(a->pformat_data);
454         tar->entry_offset = 0;
455
456         r = tar_read_header(a, tar, entry, &st);
457
458         if (r == ARCHIVE_OK) {
459                 /*
460                  * "Regular" entry with trailing '/' is really
461                  * directory: This is needed for certain old tar
462                  * variants and even for some broken newer ones.
463                  */
464                 p = archive_entry_pathname(entry);
465                 l = strlen(p);
466                 if (S_ISREG(st.st_mode) && p[l-1] == '/') {
467                         st.st_mode &= ~S_IFMT;
468                         st.st_mode |= S_IFDIR;
469                 }
470
471                 /* Copy the final stat data into the entry. */
472                 archive_entry_copy_stat(entry, &st);
473         }
474         return (r);
475 }
476
477 static int
478 archive_read_format_tar_read_data(struct archive_read *a,
479     const void **buff, size_t *size, off_t *offset)
480 {
481         ssize_t bytes_read;
482         struct tar *tar;
483         struct sparse_block *p;
484
485         tar = (struct tar *)*(a->pformat_data);
486         if (tar->sparse_list != NULL) {
487                 /* Remove exhausted entries from sparse list. */
488                 while (tar->sparse_list != NULL &&
489                     tar->sparse_list->remaining == 0) {
490                         p = tar->sparse_list;
491                         tar->sparse_list = p->next;
492                         free(p);
493                 }
494                 if (tar->sparse_list == NULL) {
495                         /* We exhausted the entire sparse list. */
496                         tar->entry_bytes_remaining = 0;
497                 }
498         }
499
500         if (tar->entry_bytes_remaining > 0) {
501                 bytes_read = (a->compression_read_ahead)(a, buff, 1);
502                 if (bytes_read == 0) {
503                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
504                             "Truncated tar archive");
505                         return (ARCHIVE_FATAL);
506                 }
507                 if (bytes_read < 0)
508                         return (ARCHIVE_FATAL);
509                 if (bytes_read > tar->entry_bytes_remaining)
510                         bytes_read = tar->entry_bytes_remaining;
511                 if (tar->sparse_list != NULL) {
512                         /* Don't read more than is available in the
513                          * current sparse block. */
514                         if (tar->sparse_list->remaining < bytes_read)
515                                 bytes_read = tar->sparse_list->remaining;
516                         tar->entry_offset = tar->sparse_list->offset;
517                         tar->sparse_list->remaining -= bytes_read;
518                         tar->sparse_list->offset += bytes_read;
519                 }
520                 *size = bytes_read;
521                 *offset = tar->entry_offset;
522                 tar->entry_offset += bytes_read;
523                 tar->entry_bytes_remaining -= bytes_read;
524                 (a->compression_read_consume)(a, bytes_read);
525                 return (ARCHIVE_OK);
526         } else {
527                 while (tar->entry_padding > 0) {
528                         bytes_read = (a->compression_read_ahead)(a, buff, 1);
529                         if (bytes_read <= 0)
530                                 return (ARCHIVE_FATAL);
531                         if (bytes_read > tar->entry_padding)
532                                 bytes_read = tar->entry_padding;
533                         (a->compression_read_consume)(a, bytes_read);
534                         tar->entry_padding -= bytes_read;
535                 }
536                 *buff = NULL;
537                 *size = 0;
538                 *offset = tar->entry_offset;
539                 return (ARCHIVE_EOF);
540         }
541 }
542
543 static int
544 archive_read_format_tar_skip(struct archive_read *a)
545 {
546         off_t bytes_skipped;
547         struct tar* tar;
548         struct sparse_block *p;
549         int r = ARCHIVE_OK;
550         const void *b;  /* dummy variables */
551         size_t s;
552         off_t o;
553
554
555         tar = (struct tar *)*(a->pformat_data);
556         if (a->compression_skip == NULL) {
557                 while (r == ARCHIVE_OK)
558                         r = archive_read_format_tar_read_data(a, &b, &s, &o);
559                 return (r);
560         }
561
562         /*
563          * Compression layer skip functions are required to either skip the
564          * length requested or fail, so we can rely upon the entire entry
565          * plus padding being skipped.
566          */
567         bytes_skipped = (a->compression_skip)(a, tar->entry_bytes_remaining +
568             tar->entry_padding);
569         if (bytes_skipped < 0)
570                 return (ARCHIVE_FATAL);
571
572         tar->entry_bytes_remaining = 0;
573         tar->entry_padding = 0;
574
575         /* Free the sparse list. */
576         while (tar->sparse_list != NULL) {
577                 p = tar->sparse_list;
578                 tar->sparse_list = p->next;
579                 free(p);
580         }
581
582         return (ARCHIVE_OK);
583 }
584
585 /*
586  * This function recursively interprets all of the headers associated
587  * with a single entry.
588  */
589 static int
590 tar_read_header(struct archive_read *a, struct tar *tar,
591     struct archive_entry *entry, struct stat *st)
592 {
593         ssize_t bytes;
594         int err;
595         const void *h;
596         const struct archive_entry_header_ustar *header;
597
598         /* Read 512-byte header record */
599         bytes = (a->compression_read_ahead)(a, &h, 512);
600         if (bytes < 512) {
601                 /*
602                  * If we're here, it's becase the _bid function accepted
603                  * this file.  So just call a short read end-of-archive
604                  * and be done with it.
605                  */
606                 return (ARCHIVE_EOF);
607         }
608         (a->compression_read_consume)(a, 512);
609
610         /* Check for end-of-archive mark. */
611         if (((*(const char *)h)==0) && archive_block_is_null((const unsigned char *)h)) {
612                 /* Try to consume a second all-null record, as well. */
613                 bytes = (a->compression_read_ahead)(a, &h, 512);
614                 if (bytes > 0)
615                         (a->compression_read_consume)(a, bytes);
616                 archive_set_error(&a->archive, 0, NULL);
617                 return (ARCHIVE_EOF);
618         }
619
620         /*
621          * Note: If the checksum fails and we return ARCHIVE_RETRY,
622          * then the client is likely to just retry.  This is a very
623          * crude way to search for the next valid header!
624          *
625          * TODO: Improve this by implementing a real header scan.
626          */
627         if (!checksum(a, h)) {
628                 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
629                 return (ARCHIVE_RETRY); /* Retryable: Invalid header */
630         }
631
632         if (++tar->header_recursion_depth > 32) {
633                 archive_set_error(&a->archive, EINVAL, "Too many special headers");
634                 return (ARCHIVE_WARN);
635         }
636
637         /* Determine the format variant. */
638         header = (const struct archive_entry_header_ustar *)h;
639         switch(header->typeflag[0]) {
640         case 'A': /* Solaris tar ACL */
641                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
642                 a->archive.archive_format_name = "Solaris tar";
643                 err = header_Solaris_ACL(a, tar, entry, st, h);
644                 break;
645         case 'g': /* POSIX-standard 'g' header. */
646                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
647                 a->archive.archive_format_name = "POSIX pax interchange format";
648                 err = header_pax_global(a, tar, entry, st, h);
649                 break;
650         case 'K': /* Long link name (GNU tar, others) */
651                 err = header_longlink(a, tar, entry, st, h);
652                 break;
653         case 'L': /* Long filename (GNU tar, others) */
654                 err = header_longname(a, tar, entry, st, h);
655                 break;
656         case 'V': /* GNU volume header */
657                 err = header_volume(a, tar, entry, st, h);
658                 break;
659         case 'X': /* Used by SUN tar; same as 'x'. */
660                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
661                 a->archive.archive_format_name =
662                     "POSIX pax interchange format (Sun variant)";
663                 err = header_pax_extensions(a, tar, entry, st, h);
664                 break;
665         case 'x': /* POSIX-standard 'x' header. */
666                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
667                 a->archive.archive_format_name = "POSIX pax interchange format";
668                 err = header_pax_extensions(a, tar, entry, st, h);
669                 break;
670         default:
671                 if (memcmp(header->magic, "ustar  \0", 8) == 0) {
672                         a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
673                         a->archive.archive_format_name = "GNU tar format";
674                         err = header_gnutar(a, tar, entry, st, h);
675                 } else if (memcmp(header->magic, "ustar", 5) == 0) {
676                         if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
677                                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
678                                 a->archive.archive_format_name = "POSIX ustar format";
679                         }
680                         err = header_ustar(a, tar, entry, st, h);
681                 } else {
682                         a->archive.archive_format = ARCHIVE_FORMAT_TAR;
683                         a->archive.archive_format_name = "tar (non-POSIX)";
684                         err = header_old_tar(a, tar, entry, st, h);
685                 }
686         }
687         --tar->header_recursion_depth;
688         return (err);
689 }
690
691 /*
692  * Return true if block checksum is correct.
693  */
694 static int
695 checksum(struct archive_read *a, const void *h)
696 {
697         const unsigned char *bytes;
698         const struct archive_entry_header_ustar *header;
699         int check, i, sum;
700
701         (void)a; /* UNUSED */
702         bytes = (const unsigned char *)h;
703         header = (const struct archive_entry_header_ustar *)h;
704
705         /*
706          * Test the checksum.  Note that POSIX specifies _unsigned_
707          * bytes for this calculation.
708          */
709         sum = tar_atol(header->checksum, sizeof(header->checksum));
710         check = 0;
711         for (i = 0; i < 148; i++)
712                 check += (unsigned char)bytes[i];
713         for (; i < 156; i++)
714                 check += 32;
715         for (; i < 512; i++)
716                 check += (unsigned char)bytes[i];
717         if (sum == check)
718                 return (1);
719
720         /*
721          * Repeat test with _signed_ bytes, just in case this archive
722          * was created by an old BSD, Solaris, or HP-UX tar with a
723          * broken checksum calculation.
724          */
725         check = 0;
726         for (i = 0; i < 148; i++)
727                 check += (signed char)bytes[i];
728         for (; i < 156; i++)
729                 check += 32;
730         for (; i < 512; i++)
731                 check += (signed char)bytes[i];
732         if (sum == check)
733                 return (1);
734
735         return (0);
736 }
737
738 /*
739  * Return true if this block contains only nulls.
740  */
741 static int
742 archive_block_is_null(const unsigned char *p)
743 {
744         unsigned i;
745
746         for (i = 0; i < ARCHIVE_BYTES_PER_RECORD / sizeof(*p); i++)
747                 if (*p++)
748                         return (0);
749         return (1);
750 }
751
752 /*
753  * Interpret 'A' Solaris ACL header
754  */
755 static int
756 header_Solaris_ACL(struct archive_read *a, struct tar *tar,
757     struct archive_entry *entry, struct stat *st, const void *h)
758 {
759         int err, err2;
760         char *p;
761         wchar_t *wp;
762
763         err = read_body_to_string(a, tar, &(tar->acl_text), h);
764         err2 = tar_read_header(a, tar, entry, st);
765         err = err_combine(err, err2);
766
767         /* XXX Ensure p doesn't overrun acl_text */
768
769         /* Skip leading octal number. */
770         /* XXX TODO: Parse the octal number and sanity-check it. */
771         p = tar->acl_text.s;
772         while (*p != '\0')
773                 p++;
774         p++;
775
776         wp = (wchar_t *)malloc((strlen(p) + 1) * sizeof(wchar_t));
777         if (wp != NULL) {
778                 utf8_decode(wp, p, strlen(p));
779                 err2 = __archive_entry_acl_parse_w(entry, wp,
780                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
781                 err = err_combine(err, err2);
782                 free(wp);
783         }
784
785         return (err);
786 }
787
788 /*
789  * Interpret 'K' long linkname header.
790  */
791 static int
792 header_longlink(struct archive_read *a, struct tar *tar,
793     struct archive_entry *entry, struct stat *st, const void *h)
794 {
795         int err, err2;
796
797         err = read_body_to_string(a, tar, &(tar->longlink), h);
798         err2 = tar_read_header(a, tar, entry, st);
799         if (err == ARCHIVE_OK && err2 == ARCHIVE_OK) {
800                 /* Set symlink if symlink already set, else hardlink. */
801                 archive_entry_set_link(entry, tar->longlink.s);
802         }
803         return (err_combine(err, err2));
804 }
805
806 /*
807  * Interpret 'L' long filename header.
808  */
809 static int
810 header_longname(struct archive_read *a, struct tar *tar,
811     struct archive_entry *entry, struct stat *st, const void *h)
812 {
813         int err, err2;
814
815         err = read_body_to_string(a, tar, &(tar->longname), h);
816         /* Read and parse "real" header, then override name. */
817         err2 = tar_read_header(a, tar, entry, st);
818         if (err == ARCHIVE_OK && err2 == ARCHIVE_OK)
819                 archive_entry_set_pathname(entry, tar->longname.s);
820         return (err_combine(err, err2));
821 }
822
823
824 /*
825  * Interpret 'V' GNU tar volume header.
826  */
827 static int
828 header_volume(struct archive_read *a, struct tar *tar,
829     struct archive_entry *entry, struct stat *st, const void *h)
830 {
831         (void)h;
832
833         /* Just skip this and read the next header. */
834         return (tar_read_header(a, tar, entry, st));
835 }
836
837 /*
838  * Read body of an archive entry into an archive_string object.
839  */
840 static int
841 read_body_to_string(struct archive_read *a, struct tar *tar,
842     struct archive_string *as, const void *h)
843 {
844         off_t size, padded_size;
845         ssize_t bytes_read, bytes_to_copy;
846         const struct archive_entry_header_ustar *header;
847         const void *src;
848         char *dest;
849
850         (void)tar; /* UNUSED */
851         header = (const struct archive_entry_header_ustar *)h;
852         size  = tar_atol(header->size, sizeof(header->size));
853
854         /* Read the body into the string. */
855         archive_string_ensure(as, size+1);
856         padded_size = (size + 511) & ~ 511;
857         dest = as->s;
858         while (padded_size > 0) {
859                 bytes_read = (a->compression_read_ahead)(a, &src, padded_size);
860                 if (bytes_read < 0)
861                         return (ARCHIVE_FATAL);
862                 if (bytes_read > padded_size)
863                         bytes_read = padded_size;
864                 (a->compression_read_consume)(a, bytes_read);
865                 bytes_to_copy = bytes_read;
866                 if ((off_t)bytes_to_copy > size)
867                         bytes_to_copy = (ssize_t)size;
868                 memcpy(dest, src, bytes_to_copy);
869                 dest += bytes_to_copy;
870                 size -= bytes_to_copy;
871                 padded_size -= bytes_read;
872         }
873         *dest = '\0';
874         return (ARCHIVE_OK);
875 }
876
877 /*
878  * Parse out common header elements.
879  *
880  * This would be the same as header_old_tar, except that the
881  * filename is handled slightly differently for old and POSIX
882  * entries  (POSIX entries support a 'prefix').  This factoring
883  * allows header_old_tar and header_ustar
884  * to handle filenames differently, while still putting most of the
885  * common parsing into one place.
886  */
887 static int
888 header_common(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
889     struct stat *st, const void *h)
890 {
891         const struct archive_entry_header_ustar *header;
892         char    tartype;
893
894         (void)a; /* UNUSED */
895
896         header = (const struct archive_entry_header_ustar *)h;
897         if (header->linkname[0])
898                 archive_strncpy(&(tar->entry_linkname), header->linkname,
899                     sizeof(header->linkname));
900         else
901                 archive_string_empty(&(tar->entry_linkname));
902
903         /* Parse out the numeric fields (all are octal) */
904         st->st_mode  = tar_atol(header->mode, sizeof(header->mode));
905         st->st_uid   = tar_atol(header->uid, sizeof(header->uid));
906         st->st_gid   = tar_atol(header->gid, sizeof(header->gid));
907         st->st_size  = tar_atol(header->size, sizeof(header->size));
908         st->st_mtime = tar_atol(header->mtime, sizeof(header->mtime));
909
910         /* Handle the tar type flag appropriately. */
911         tartype = header->typeflag[0];
912         st->st_mode &= ~S_IFMT;
913
914         switch (tartype) {
915         case '1': /* Hard link */
916                 archive_entry_set_hardlink(entry, tar->entry_linkname.s);
917                 /*
918                  * The following may seem odd, but: Technically, tar
919                  * does not store the file type for a "hard link"
920                  * entry, only the fact that it is a hard link.  So, I
921                  * leave the type zero normally.  But, pax interchange
922                  * format allows hard links to have data, which
923                  * implies that the underlying entry is a regular
924                  * file.
925                  */
926                 if (st->st_size > 0)
927                         st->st_mode |= S_IFREG;
928
929                 /*
930                  * A tricky point: Traditionally, tar readers have
931                  * ignored the size field when reading hardlink
932                  * entries, and some writers put non-zero sizes even
933                  * though the body is empty.  POSIX.1-2001 broke with
934                  * this tradition by permitting hardlink entries to
935                  * store valid bodies in pax interchange format, but
936                  * not in ustar format.  Since there is no hard and
937                  * fast way to distinguish pax interchange from
938                  * earlier archives (the 'x' and 'g' entries are
939                  * optional, after all), we need a heuristic.  Here, I
940                  * use the bid function to test whether or not there's
941                  * a valid header following.  Of course, if we know
942                  * this is pax interchange format, then we must obey
943                  * the size.
944                  *
945                  * This heuristic will only fail for a pax interchange
946                  * archive that is storing hardlink bodies, no pax
947                  * extended attribute entries have yet occurred, and
948                  * we encounter a hardlink entry for a file that is
949                  * itself an uncompressed tar archive.
950                  */
951                 if (st->st_size > 0  &&
952                     a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE  &&
953                     archive_read_format_tar_bid(a) > 50)
954                         st->st_size = 0;
955                 break;
956         case '2': /* Symlink */
957                 st->st_mode |= S_IFLNK;
958                 st->st_size = 0;
959                 archive_entry_set_symlink(entry, tar->entry_linkname.s);
960                 break;
961         case '3': /* Character device */
962                 st->st_mode |= S_IFCHR;
963                 st->st_size = 0;
964                 break;
965         case '4': /* Block device */
966                 st->st_mode |= S_IFBLK;
967                 st->st_size = 0;
968                 break;
969         case '5': /* Dir */
970                 st->st_mode |= S_IFDIR;
971                 st->st_size = 0;
972                 break;
973         case '6': /* FIFO device */
974                 st->st_mode |= S_IFIFO;
975                 st->st_size = 0;
976                 break;
977         case 'D': /* GNU incremental directory type */
978                 /*
979                  * No special handling is actually required here.
980                  * It might be nice someday to preprocess the file list and
981                  * provide it to the client, though.
982                  */
983                 st->st_mode |= S_IFDIR;
984                 break;
985         case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
986                 /*
987                  * As far as I can tell, this is just like a regular file
988                  * entry, except that the contents should be _appended_ to
989                  * the indicated file at the indicated offset.  This may
990                  * require some API work to fully support.
991                  */
992                 break;
993         case 'N': /* Old GNU "long filename" entry. */
994                 /* The body of this entry is a script for renaming
995                  * previously-extracted entries.  Ugh.  It will never
996                  * be supported by libarchive. */
997                 st->st_mode |= S_IFREG;
998                 break;
999         case 'S': /* GNU sparse files */
1000                 /*
1001                  * Sparse files are really just regular files with
1002                  * sparse information in the extended area.
1003                  */
1004                 /* FALL THROUGH */
1005         default: /* Regular file  and non-standard types */
1006                 /*
1007                  * Per POSIX: non-recognized types should always be
1008                  * treated as regular files.
1009                  */
1010                 st->st_mode |= S_IFREG;
1011                 break;
1012         }
1013         return (0);
1014 }
1015
1016 /*
1017  * Parse out header elements for "old-style" tar archives.
1018  */
1019 static int
1020 header_old_tar(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1021     struct stat *st, const void *h)
1022 {
1023         const struct archive_entry_header_ustar *header;
1024
1025         /* Copy filename over (to ensure null termination). */
1026         header = (const struct archive_entry_header_ustar *)h;
1027         archive_strncpy(&(tar->entry_name), header->name, sizeof(header->name));
1028         archive_entry_set_pathname(entry, tar->entry_name.s);
1029
1030         /* Grab rest of common fields */
1031         header_common(a, tar, entry, st, h);
1032
1033         tar->entry_bytes_remaining = st->st_size;
1034         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1035         return (0);
1036 }
1037
1038 /*
1039  * Parse a file header for a pax extended archive entry.
1040  */
1041 static int
1042 header_pax_global(struct archive_read *a, struct tar *tar,
1043     struct archive_entry *entry, struct stat *st, const void *h)
1044 {
1045         int err, err2;
1046
1047         err = read_body_to_string(a, tar, &(tar->pax_global), h);
1048         err2 = tar_read_header(a, tar, entry, st);
1049         return (err_combine(err, err2));
1050 }
1051
1052 static int
1053 header_pax_extensions(struct archive_read *a, struct tar *tar,
1054     struct archive_entry *entry, struct stat *st, const void *h)
1055 {
1056         int err, err2;
1057
1058         read_body_to_string(a, tar, &(tar->pax_header), h);
1059
1060         /* Parse the next header. */
1061         err = tar_read_header(a, tar, entry, st);
1062
1063         /*
1064          * TODO: Parse global/default options into 'entry' struct here
1065          * before handling file-specific options.
1066          *
1067          * This design (parse standard header, then overwrite with pax
1068          * extended attribute data) usually works well, but isn't ideal;
1069          * it would be better to parse the pax extended attributes first
1070          * and then skip any fields in the standard header that were
1071          * defined in the pax header.
1072          */
1073         err2 = pax_header(a, tar, entry, st, tar->pax_header.s);
1074         err =  err_combine(err, err2);
1075         tar->entry_bytes_remaining = st->st_size;
1076         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1077         return (err);
1078 }
1079
1080
1081 /*
1082  * Parse a file header for a Posix "ustar" archive entry.  This also
1083  * handles "pax" or "extended ustar" entries.
1084  */
1085 static int
1086 header_ustar(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1087     struct stat *st, const void *h)
1088 {
1089         const struct archive_entry_header_ustar *header;
1090         struct archive_string *as;
1091
1092         header = (const struct archive_entry_header_ustar *)h;
1093
1094         /* Copy name into an internal buffer to ensure null-termination. */
1095         as = &(tar->entry_name);
1096         if (header->prefix[0]) {
1097                 archive_strncpy(as, header->prefix, sizeof(header->prefix));
1098                 if (as->s[archive_strlen(as) - 1] != '/')
1099                         archive_strappend_char(as, '/');
1100                 archive_strncat(as, header->name, sizeof(header->name));
1101         } else
1102                 archive_strncpy(as, header->name, sizeof(header->name));
1103
1104         archive_entry_set_pathname(entry, as->s);
1105
1106         /* Handle rest of common fields. */
1107         header_common(a, tar, entry, st, h);
1108
1109         /* Handle POSIX ustar fields. */
1110         archive_strncpy(&(tar->entry_uname), header->uname,
1111             sizeof(header->uname));
1112         archive_entry_set_uname(entry, tar->entry_uname.s);
1113
1114         archive_strncpy(&(tar->entry_gname), header->gname,
1115             sizeof(header->gname));
1116         archive_entry_set_gname(entry, tar->entry_gname.s);
1117
1118         /* Parse out device numbers only for char and block specials. */
1119         if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1120                 st->st_rdev = makedev(
1121                     tar_atol(header->rdevmajor, sizeof(header->rdevmajor)),
1122                     tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1123         }
1124
1125         tar->entry_bytes_remaining = st->st_size;
1126         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1127
1128         return (0);
1129 }
1130
1131
1132 /*
1133  * Parse the pax extended attributes record.
1134  *
1135  * Returns non-zero if there's an error in the data.
1136  */
1137 static int
1138 pax_header(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1139     struct stat *st, char *attr)
1140 {
1141         size_t attr_length, l, line_length;
1142         char *line, *p;
1143         wchar_t *key, *wp, *value;
1144         int err, err2;
1145
1146         attr_length = strlen(attr);
1147         err = ARCHIVE_OK;
1148         while (attr_length > 0) {
1149                 /* Parse decimal length field at start of line. */
1150                 line_length = 0;
1151                 l = attr_length;
1152                 line = p = attr; /* Record start of line. */
1153                 while (l>0) {
1154                         if (*p == ' ') {
1155                                 p++;
1156                                 l--;
1157                                 break;
1158                         }
1159                         if (*p < '0' || *p > '9')
1160                                 return (-1);
1161                         line_length *= 10;
1162                         line_length += *p - '0';
1163                         if (line_length > 999999) {
1164                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1165                                     "Rejecting pax extended attribute > 1MB");
1166                                 return (ARCHIVE_WARN);
1167                         }
1168                         p++;
1169                         l--;
1170                 }
1171
1172                 if (line_length > attr_length)
1173                         return (0);
1174
1175                 /* Ensure pax_entry buffer is big enough. */
1176                 if (tar->pax_entry_length <= line_length) {
1177                         wchar_t *old_entry = tar->pax_entry;
1178
1179                         if (tar->pax_entry_length <= 0)
1180                                 tar->pax_entry_length = 1024;
1181                         while (tar->pax_entry_length <= line_length + 1)
1182                                 tar->pax_entry_length *= 2;
1183
1184                         old_entry = tar->pax_entry;
1185                         tar->pax_entry = (wchar_t *)realloc(tar->pax_entry,
1186                             tar->pax_entry_length * sizeof(wchar_t));
1187                         if (tar->pax_entry == NULL) {
1188                                 free(old_entry);
1189                                 archive_set_error(&a->archive, ENOMEM,
1190                                         "No memory");
1191                                 return (ARCHIVE_FATAL);
1192                         }
1193                 }
1194
1195                 /* Decode UTF-8 to wchar_t, null-terminate result. */
1196                 if (utf8_decode(tar->pax_entry, p,
1197                         line_length - (p - attr) - 1)) {
1198                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1199                            "Invalid UTF8 character in pax extended attribute");
1200                         err = err_combine(err, ARCHIVE_WARN);
1201                 }
1202
1203                 /* Null-terminate 'key' value. */
1204                 wp = key = tar->pax_entry;
1205                 if (key[0] == L'=')
1206                         return (-1);
1207                 while (*wp && *wp != L'=')
1208                         ++wp;
1209                 if (*wp == L'\0' || wp == NULL) {
1210                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1211                             "Invalid pax extended attributes");
1212                         return (ARCHIVE_WARN);
1213                 }
1214                 *wp = 0;
1215
1216                 /* Identify null-terminated 'value' portion. */
1217                 value = wp + 1;
1218
1219                 /* Identify this attribute and set it in the entry. */
1220                 err2 = pax_attribute(entry, st, key, value);
1221                 err = err_combine(err, err2);
1222
1223                 /* Skip to next line */
1224                 attr += line_length;
1225                 attr_length -= line_length;
1226         }
1227         return (err);
1228 }
1229
1230 static int
1231 pax_attribute_xattr(struct archive_entry *entry,
1232         wchar_t *name, wchar_t *value)
1233 {
1234         char *name_decoded, *name_narrow;
1235         void *value_decoded;
1236         size_t value_len;
1237
1238         if (wcslen(name) < 18 || (wcsncmp(name, L"LIBARCHIVE.xattr.", 17)) != 0)
1239                 return 3;
1240
1241         name += 17;
1242
1243         /* URL-decode name */
1244         name_narrow = wide_to_narrow(name);
1245         if (name_narrow == NULL)
1246                 return 2;
1247         name_decoded = url_decode(name_narrow);
1248         free(name_narrow);
1249         if (name_decoded == NULL)
1250                 return 2;
1251
1252         /* Base-64 decode value */
1253         value_decoded = base64_decode(value, wcslen(value), &value_len);
1254         if (value_decoded == NULL) {
1255                 free(name_decoded);
1256                 return 1;
1257         }
1258
1259         archive_entry_xattr_add_entry(entry, name_decoded,
1260                 value_decoded, value_len);
1261
1262         free(name_decoded);
1263         free(value_decoded);
1264         return 0;
1265 }
1266
1267 /*
1268  * Parse a single key=value attribute.  key/value pointers are
1269  * assumed to point into reasonably long-lived storage.
1270  *
1271  * Note that POSIX reserves all-lowercase keywords.  Vendor-specific
1272  * extensions should always have keywords of the form "VENDOR.attribute"
1273  * In particular, it's quite feasible to support many different
1274  * vendor extensions here.  I'm using "LIBARCHIVE" for extensions
1275  * unique to this library (currently, there are none).
1276  *
1277  * Investigate other vendor-specific extensions, as well and see if
1278  * any of them look useful.
1279  */
1280 static int
1281 pax_attribute(struct archive_entry *entry, struct stat *st,
1282     wchar_t *key, wchar_t *value)
1283 {
1284         int64_t s;
1285         long n;
1286
1287         switch (key[0]) {
1288         case 'L':
1289                 /* Our extensions */
1290 /* TODO: Handle arbitrary extended attributes... */
1291 /*
1292                 if (strcmp(key, "LIBARCHIVE.xxxxxxx")==0)
1293                         archive_entry_set_xxxxxx(entry, value);
1294 */
1295                 if (wcsncmp(key, L"LIBARCHIVE.xattr.", 17)==0)
1296                         pax_attribute_xattr(entry, key, value);
1297                 break;
1298         case 'S':
1299                 /* We support some keys used by the "star" archiver */
1300                 if (wcscmp(key, L"SCHILY.acl.access")==0)
1301                         __archive_entry_acl_parse_w(entry, value,
1302                             ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
1303                 else if (wcscmp(key, L"SCHILY.acl.default")==0)
1304                         __archive_entry_acl_parse_w(entry, value,
1305                             ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1306                 else if (wcscmp(key, L"SCHILY.devmajor")==0)
1307                         st->st_rdev = makedev(tar_atol10(value, wcslen(value)),
1308                             minor(st->st_rdev));
1309                 else if (wcscmp(key, L"SCHILY.devminor")==0)
1310                         st->st_rdev = makedev(major(st->st_rdev),
1311                             tar_atol10(value, wcslen(value)));
1312                 else if (wcscmp(key, L"SCHILY.fflags")==0)
1313                         archive_entry_copy_fflags_text_w(entry, value);
1314                 else if (wcscmp(key, L"SCHILY.nlink")==0)
1315                         st->st_nlink = tar_atol10(value, wcslen(value));
1316                 break;
1317         case 'a':
1318                 if (wcscmp(key, L"atime")==0) {
1319                         pax_time(value, &s, &n);
1320                         st->st_atime = s;
1321                         ARCHIVE_STAT_SET_ATIME_NANOS(st, n);
1322                 }
1323                 break;
1324         case 'c':
1325                 if (wcscmp(key, L"ctime")==0) {
1326                         pax_time(value, &s, &n);
1327                         st->st_ctime = s;
1328                         ARCHIVE_STAT_SET_CTIME_NANOS(st, n);
1329                 } else if (wcscmp(key, L"charset")==0) {
1330                         /* TODO: Publish charset information in entry. */
1331                 } else if (wcscmp(key, L"comment")==0) {
1332                         /* TODO: Publish comment in entry. */
1333                 }
1334                 break;
1335         case 'g':
1336                 if (wcscmp(key, L"gid")==0)
1337                         st->st_gid = tar_atol10(value, wcslen(value));
1338                 else if (wcscmp(key, L"gname")==0)
1339                         archive_entry_copy_gname_w(entry, value);
1340                 break;
1341         case 'l':
1342                 /* pax interchange doesn't distinguish hardlink vs. symlink. */
1343                 if (wcscmp(key, L"linkpath")==0) {
1344                         if (archive_entry_hardlink(entry))
1345                                 archive_entry_copy_hardlink_w(entry, value);
1346                         else
1347                                 archive_entry_copy_symlink_w(entry, value);
1348                 }
1349                 break;
1350         case 'm':
1351                 if (wcscmp(key, L"mtime")==0) {
1352                         pax_time(value, &s, &n);
1353                         st->st_mtime = s;
1354                         ARCHIVE_STAT_SET_MTIME_NANOS(st, n);
1355                 }
1356                 break;
1357         case 'p':
1358                 if (wcscmp(key, L"path")==0)
1359                         archive_entry_copy_pathname_w(entry, value);
1360                 break;
1361         case 'r':
1362                 /* POSIX has reserved 'realtime.*' */
1363                 break;
1364         case 's':
1365                 /* POSIX has reserved 'security.*' */
1366                 /* Someday: if (wcscmp(key, L"security.acl")==0) { ... } */
1367                 if (wcscmp(key, L"size")==0)
1368                         st->st_size = tar_atol10(value, wcslen(value));
1369                 break;
1370         case 'u':
1371                 if (wcscmp(key, L"uid")==0)
1372                         st->st_uid = tar_atol10(value, wcslen(value));
1373                 else if (wcscmp(key, L"uname")==0)
1374                         archive_entry_copy_uname_w(entry, value);
1375                 break;
1376         }
1377         return (0);
1378 }
1379
1380
1381
1382 /*
1383  * parse a decimal time value, which may include a fractional portion
1384  */
1385 static void
1386 pax_time(const wchar_t *p, int64_t *ps, long *pn)
1387 {
1388         char digit;
1389         int64_t s;
1390         unsigned long l;
1391         int sign;
1392         int64_t limit, last_digit_limit;
1393
1394         limit = max_int64 / 10;
1395         last_digit_limit = max_int64 % 10;
1396
1397         s = 0;
1398         sign = 1;
1399         if (*p == '-') {
1400                 sign = -1;
1401                 p++;
1402         }
1403         while (*p >= '0' && *p <= '9') {
1404                 digit = *p - '0';
1405                 if (s > limit ||
1406                     (s == limit && digit > last_digit_limit)) {
1407                         s = max_uint64;
1408                         break;
1409                 }
1410                 s = (s * 10) + digit;
1411                 ++p;
1412         }
1413
1414         *ps = s * sign;
1415
1416         /* Calculate nanoseconds. */
1417         *pn = 0;
1418
1419         if (*p != '.')
1420                 return;
1421
1422         l = 100000000UL;
1423         do {
1424                 ++p;
1425                 if (*p >= '0' && *p <= '9')
1426                         *pn += (*p - '0') * l;
1427                 else
1428                         break;
1429         } while (l /= 10);
1430 }
1431
1432 /*
1433  * Parse GNU tar header
1434  */
1435 static int
1436 header_gnutar(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1437     struct stat *st, const void *h)
1438 {
1439         const struct archive_entry_header_gnutar *header;
1440
1441         (void)a;
1442
1443         /*
1444          * GNU header is like POSIX ustar, except 'prefix' is
1445          * replaced with some other fields. This also means the
1446          * filename is stored as in old-style archives.
1447          */
1448
1449         /* Grab fields common to all tar variants. */
1450         header_common(a, tar, entry, st, h);
1451
1452         /* Copy filename over (to ensure null termination). */
1453         header = (const struct archive_entry_header_gnutar *)h;
1454         archive_strncpy(&(tar->entry_name), header->name,
1455             sizeof(header->name));
1456         archive_entry_set_pathname(entry, tar->entry_name.s);
1457
1458         /* Fields common to ustar and GNU */
1459         /* XXX Can the following be factored out since it's common
1460          * to ustar and gnu tar?  Is it okay to move it down into
1461          * header_common, perhaps?  */
1462         archive_strncpy(&(tar->entry_uname),
1463             header->uname, sizeof(header->uname));
1464         archive_entry_set_uname(entry, tar->entry_uname.s);
1465
1466         archive_strncpy(&(tar->entry_gname),
1467             header->gname, sizeof(header->gname));
1468         archive_entry_set_gname(entry, tar->entry_gname.s);
1469
1470         /* Parse out device numbers only for char and block specials */
1471         if (header->typeflag[0] == '3' || header->typeflag[0] == '4')
1472                 st->st_rdev = makedev (
1473                     tar_atol(header->rdevmajor, sizeof(header->rdevmajor)),
1474                     tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1475         else
1476                 st->st_rdev = 0;
1477
1478         tar->entry_bytes_remaining = st->st_size;
1479         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1480
1481         /* Grab GNU-specific fields. */
1482         st->st_atime = tar_atol(header->atime, sizeof(header->atime));
1483         st->st_ctime = tar_atol(header->ctime, sizeof(header->ctime));
1484         if (header->realsize[0] != 0) {
1485                 st->st_size = tar_atol(header->realsize,
1486                     sizeof(header->realsize));
1487         }
1488
1489         if (header->sparse[0].offset[0] != 0) {
1490                 gnu_read_sparse_data(a, tar, header);
1491         } else {
1492                 if (header->isextended[0] != 0) {
1493                         /* XXX WTF? XXX */
1494                 }
1495         }
1496
1497         return (0);
1498 }
1499
1500 static int
1501 gnu_read_sparse_data(struct archive_read *a, struct tar *tar,
1502     const struct archive_entry_header_gnutar *header)
1503 {
1504         ssize_t bytes_read;
1505         const void *data;
1506         struct extended {
1507                 struct gnu_sparse sparse[21];
1508                 char    isextended[1];
1509                 char    padding[7];
1510         };
1511         const struct extended *ext;
1512
1513         gnu_parse_sparse_data(a, tar, header->sparse, 4);
1514         if (header->isextended[0] == 0)
1515                 return (ARCHIVE_OK);
1516
1517         do {
1518                 bytes_read = (a->compression_read_ahead)(a, &data, 512);
1519                 if (bytes_read < 0)
1520                         return (ARCHIVE_FATAL);
1521                 if (bytes_read < 512) {
1522                         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1523                             "Truncated tar archive "
1524                             "detected while reading sparse file data");
1525                         return (ARCHIVE_FATAL);
1526                 }
1527                 (a->compression_read_consume)(a, 512);
1528                 ext = (const struct extended *)data;
1529                 gnu_parse_sparse_data(a, tar, ext->sparse, 21);
1530         } while (ext->isextended[0] != 0);
1531         if (tar->sparse_list != NULL)
1532                 tar->entry_offset = tar->sparse_list->offset;
1533         return (ARCHIVE_OK);
1534 }
1535
1536 static void
1537 gnu_parse_sparse_data(struct archive_read *a, struct tar *tar,
1538     const struct gnu_sparse *sparse, int length)
1539 {
1540         struct sparse_block *last;
1541         struct sparse_block *p;
1542
1543         (void)a; /* UNUSED */
1544
1545         last = tar->sparse_list;
1546         while (last != NULL && last->next != NULL)
1547                 last = last->next;
1548
1549         while (length > 0 && sparse->offset[0] != 0) {
1550                 p = (struct sparse_block *)malloc(sizeof(*p));
1551                 if (p == NULL)
1552                         __archive_errx(1, "Out of memory");
1553                 memset(p, 0, sizeof(*p));
1554                 if (last != NULL)
1555                         last->next = p;
1556                 else
1557                         tar->sparse_list = p;
1558                 last = p;
1559                 p->offset = tar_atol(sparse->offset, sizeof(sparse->offset));
1560                 p->remaining =
1561                     tar_atol(sparse->numbytes, sizeof(sparse->numbytes));
1562                 sparse++;
1563                 length--;
1564         }
1565 }
1566
1567 /*-
1568  * Convert text->integer.
1569  *
1570  * Traditional tar formats (including POSIX) specify base-8 for
1571  * all of the standard numeric fields.  This is a significant limitation
1572  * in practice:
1573  *   = file size is limited to 8GB
1574  *   = rdevmajor and rdevminor are limited to 21 bits
1575  *   = uid/gid are limited to 21 bits
1576  *
1577  * There are two workarounds for this:
1578  *   = pax extended headers, which use variable-length string fields
1579  *   = GNU tar and STAR both allow either base-8 or base-256 in
1580  *      most fields.  The high bit is set to indicate base-256.
1581  *
1582  * On read, this implementation supports both extensions.
1583  */
1584 static int64_t
1585 tar_atol(const char *p, unsigned char_cnt)
1586 {
1587         /*
1588          * Technically, GNU tar considers a field to be in base-256
1589          * only if the first byte is 0xff or 0x80.
1590          */
1591         if (*p & 0x80)
1592                 return (tar_atol256(p, char_cnt));
1593         return (tar_atol8(p, char_cnt));
1594 }
1595
1596 /*
1597  * Note that this implementation does not (and should not!) obey
1598  * locale settings; you cannot simply substitute strtol here, since
1599  * it does obey locale.
1600  */
1601 static int64_t
1602 tar_atol8(const char *p, unsigned char_cnt)
1603 {
1604         int64_t l, limit, last_digit_limit;
1605         int digit, sign, base;
1606
1607         base = 8;
1608         limit = max_int64 / base;
1609         last_digit_limit = max_int64 % base;
1610
1611         while (*p == ' ' || *p == '\t')
1612                 p++;
1613         if (*p == '-') {
1614                 sign = -1;
1615                 p++;
1616         } else
1617                 sign = 1;
1618
1619         l = 0;
1620         digit = *p - '0';
1621         while (digit >= 0 && digit < base  && char_cnt-- > 0) {
1622                 if (l>limit || (l == limit && digit > last_digit_limit)) {
1623                         l = max_uint64; /* Truncate on overflow. */
1624                         break;
1625                 }
1626                 l = (l * base) + digit;
1627                 digit = *++p - '0';
1628         }
1629         return (sign < 0) ? -l : l;
1630 }
1631
1632
1633 /*
1634  * Note that this implementation does not (and should not!) obey
1635  * locale settings; you cannot simply substitute strtol here, since
1636  * it does obey locale.
1637  */
1638 static int64_t
1639 tar_atol10(const wchar_t *p, unsigned char_cnt)
1640 {
1641         int64_t l, limit, last_digit_limit;
1642         int base, digit, sign;
1643
1644         base = 10;
1645         limit = max_int64 / base;
1646         last_digit_limit = max_int64 % base;
1647
1648         while (*p == ' ' || *p == '\t')
1649                 p++;
1650         if (*p == '-') {
1651                 sign = -1;
1652                 p++;
1653         } else
1654                 sign = 1;
1655
1656         l = 0;
1657         digit = *p - '0';
1658         while (digit >= 0 && digit < base  && char_cnt-- > 0) {
1659                 if (l > limit || (l == limit && digit > last_digit_limit)) {
1660                         l = max_uint64; /* Truncate on overflow. */
1661                         break;
1662                 }
1663                 l = (l * base) + digit;
1664                 digit = *++p - '0';
1665         }
1666         return (sign < 0) ? -l : l;
1667 }
1668
1669 /*
1670  * Parse a base-256 integer.  This is just a straight signed binary
1671  * value in big-endian order, except that the high-order bit is
1672  * ignored.  Remember that "int64_t" may or may not be exactly 64
1673  * bits; the implementation here tries to avoid making any assumptions
1674  * about the actual size of an int64_t.  It does assume we're using
1675  * twos-complement arithmetic, though.
1676  */
1677 static int64_t
1678 tar_atol256(const char *_p, unsigned char_cnt)
1679 {
1680         int64_t l, upper_limit, lower_limit;
1681         const unsigned char *p = (const unsigned char *)_p;
1682
1683         upper_limit = max_int64 / 256;
1684         lower_limit = min_int64 / 256;
1685
1686         /* Pad with 1 or 0 bits, depending on sign. */
1687         if ((0x40 & *p) == 0x40)
1688                 l = (int64_t)-1;
1689         else
1690                 l = 0;
1691         l = (l << 6) | (0x3f & *p++);
1692         while (--char_cnt > 0) {
1693                 if (l > upper_limit) {
1694                         l = max_int64; /* Truncate on overflow */
1695                         break;
1696                 } else if (l < lower_limit) {
1697                         l = min_int64;
1698                         break;
1699                 }
1700                 l = (l << 8) | (0xff & (int64_t)*p++);
1701         }
1702         return (l);
1703 }
1704
1705 static int
1706 utf8_decode(wchar_t *dest, const char *src, size_t length)
1707 {
1708         size_t n;
1709         int err;
1710
1711         err = 0;
1712         while (length > 0) {
1713                 n = UTF8_mbrtowc(dest, src, length);
1714                 if (n == 0)
1715                         break;
1716                 dest++;
1717                 src += n;
1718                 length -= n;
1719         }
1720         *dest++ = L'\0';
1721         return (err);
1722 }
1723
1724 /*
1725  * Copied and simplified from FreeBSD libc/locale.
1726  */
1727 static size_t
1728 UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n)
1729 {
1730         int ch, i, len, mask;
1731         unsigned long wch;
1732
1733         if (s == NULL || n == 0 || pwc == NULL)
1734                 return (0);
1735
1736         /*
1737          * Determine the number of octets that make up this character from
1738          * the first octet, and a mask that extracts the interesting bits of
1739          * the first octet.
1740          */
1741         ch = (unsigned char)*s;
1742         if ((ch & 0x80) == 0) {
1743                 mask = 0x7f;
1744                 len = 1;
1745         } else if ((ch & 0xe0) == 0xc0) {
1746                 mask = 0x1f;
1747                 len = 2;
1748         } else if ((ch & 0xf0) == 0xe0) {
1749                 mask = 0x0f;
1750                 len = 3;
1751         } else if ((ch & 0xf8) == 0xf0) {
1752                 mask = 0x07;
1753                 len = 4;
1754         } else if ((ch & 0xfc) == 0xf8) {
1755                 mask = 0x03;
1756                 len = 5;
1757         } else if ((ch & 0xfe) == 0xfc) {
1758                 mask = 0x01;
1759                 len = 6;
1760         } else {
1761                 /* Invalid first byte; convert to '?' */
1762                 *pwc = '?';
1763                 return (1);
1764         }
1765
1766         if (n < (size_t)len) {
1767                 /* Invalid first byte; convert to '?' */
1768                 *pwc = '?';
1769                 return (1);
1770         }
1771
1772         /*
1773          * Decode the octet sequence representing the character in chunks
1774          * of 6 bits, most significant first.
1775          */
1776         wch = (unsigned char)*s++ & mask;
1777         i = len;
1778         while (--i != 0) {
1779                 if ((*s & 0xc0) != 0x80) {
1780                         /* Invalid intermediate byte; consume one byte and
1781                          * emit '?' */
1782                         *pwc = '?';
1783                         return (1);
1784                 }
1785                 wch <<= 6;
1786                 wch |= *s++ & 0x3f;
1787         }
1788
1789         /* Assign the value to the output; out-of-range values
1790          * just get truncated. */
1791         *pwc = (wchar_t)wch;
1792 #ifdef WCHAR_MAX
1793         /*
1794          * If platform has WCHAR_MAX, we can do something
1795          * more sensible with out-of-range values.
1796          */
1797         if (wch >= WCHAR_MAX)
1798                 *pwc = '?';
1799 #endif
1800         /* Return number of bytes input consumed: 0 for end-of-string. */
1801         return (wch == L'\0' ? 0 : len);
1802 }
1803
1804
1805 /*
1806  * base64_decode - Base64 decode
1807  *
1808  * This accepts most variations of base-64 encoding, including:
1809  *    * with or without line breaks
1810  *    * with or without the final group padded with '=' or '_' characters
1811  * (The most economical Base-64 variant does not pad the last group and
1812  * omits line breaks; RFC1341 used for MIME requires both.)
1813  */
1814 static char *
1815 base64_decode(const wchar_t *src, size_t len, size_t *out_len)
1816 {
1817         static const unsigned char digits[64] = {
1818                 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
1819                 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
1820                 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
1821                 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
1822                 '4','5','6','7','8','9','+','/' };
1823         static unsigned char decode_table[128];
1824         char *out, *d;
1825
1826         /* If the decode table is not yet initialized, prepare it. */
1827         if (decode_table[digits[1]] != 1) {
1828                 size_t i;
1829                 memset(decode_table, 0xff, sizeof(decode_table));
1830                 for (i = 0; i < sizeof(digits); i++)
1831                         decode_table[digits[i]] = i;
1832         }
1833
1834         /* Allocate enough space to hold the entire output. */
1835         /* Note that we may not use all of this... */
1836         out = (char *)malloc((len * 3 + 3) / 4);
1837         if (out == NULL) {
1838                 *out_len = 0;
1839                 return (NULL);
1840         }
1841         d = out;
1842
1843         while (len > 0) {
1844                 /* Collect the next group of (up to) four characters. */
1845                 int v = 0;
1846                 int group_size = 0;
1847                 while (group_size < 4 && len > 0) {
1848                         /* '=' or '_' padding indicates final group. */
1849                         if (*src == '=' || *src == '_') {
1850                                 len = 0;
1851                                 break;
1852                         }
1853                         /* Skip illegal characters (including line breaks) */
1854                         if (*src > 127 || *src < 32
1855                             || decode_table[*src] == 0xff) {
1856                                 len--;
1857                                 src++;
1858                                 continue;
1859                         }
1860                         v <<= 6;
1861                         v |= decode_table[*src++];
1862                         len --;
1863                         group_size++;
1864                 }
1865                 /* Align a short group properly. */
1866                 v <<= 6 * (4 - group_size);
1867                 /* Unpack the group we just collected. */
1868                 switch (group_size) {
1869                 case 4: d[2] = v & 0xff;
1870                         /* FALLTHROUGH */
1871                 case 3: d[1] = (v >> 8) & 0xff;
1872                         /* FALLTHROUGH */
1873                 case 2: d[0] = (v >> 16) & 0xff;
1874                         break;
1875                 case 1: /* this is invalid! */
1876                         break;
1877                 }
1878                 d += group_size * 3 / 4;
1879         }
1880
1881         *out_len = d - out;
1882         return (out);
1883 }
1884
1885 /*
1886  * This is a little tricky because the C99 standard wcstombs()
1887  * function returns the number of bytes that were converted,
1888  * not the number that should be converted.  As a result,
1889  * we can never accurately size the output buffer (without
1890  * doing a tedious output size calculation in advance).
1891  * This approach (try a conversion, then try again if it fails)
1892  * will almost always succeed on the first try, and is thus
1893  * much faster, at the cost of sometimes requiring multiple
1894  * passes while we expand the buffer.
1895  */
1896 static char *
1897 wide_to_narrow(const wchar_t *wval)
1898 {
1899         int converted_length;
1900         /* Guess an output buffer size and try the conversion. */
1901         int alloc_length = wcslen(wval) * 3;
1902         char *mbs_val = (char *)malloc(alloc_length + 1);
1903         if (mbs_val == NULL)
1904                 return (NULL);
1905         converted_length = wcstombs(mbs_val, wval, alloc_length);
1906
1907         /* If we exhausted the buffer, resize and try again. */
1908         while (converted_length >= alloc_length) {
1909                 free(mbs_val);
1910                 alloc_length *= 2;
1911                 mbs_val = (char *)malloc(alloc_length + 1);
1912                 if (mbs_val == NULL)
1913                         return (NULL);
1914                 converted_length = wcstombs(mbs_val, wval, alloc_length);
1915         }
1916
1917         /* Ensure a trailing null and return the final string. */
1918         mbs_val[alloc_length] = '\0';
1919         return (mbs_val);
1920 }
1921
1922 static char *
1923 url_decode(const char *in)
1924 {
1925         char *out, *d;
1926         const char *s;
1927
1928         out = (char *)malloc(strlen(in) + 1);
1929         if (out == NULL)
1930                 return (NULL);
1931         for (s = in, d = out; *s != '\0'; ) {
1932                 if (*s == '%') {
1933                         /* Try to convert % escape */
1934                         int digit1 = tohex(s[1]);
1935                         int digit2 = tohex(s[2]);
1936                         if (digit1 >= 0 && digit2 >= 0) {
1937                                 /* Looks good, consume three chars */
1938                                 s += 3;
1939                                 /* Convert output */
1940                                 *d++ = ((digit1 << 4) | digit2);
1941                                 continue;
1942                         }
1943                         /* Else fall through and treat '%' as normal char */
1944                 }
1945                 *d++ = *s++;
1946         }
1947         *d = '\0';
1948         return (out);
1949 }
1950
1951 static int
1952 tohex(int c)
1953 {
1954         if (c >= '0' && c <= '9')
1955                 return (c - '0');
1956         else if (c >= 'A' && c <= 'F')
1957                 return (c - 'A' + 10);
1958         else if (c >= 'a' && c <= 'f')
1959                 return (c - 'a' + 10);
1960         else
1961                 return (-1);
1962 }