Import libarchive-2.5.5.
[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.52 2007/04/03 23:53:55 cperciva 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                 if ((a->compression_skip)(a, tar->entry_padding) < 0)
528                         return (ARCHIVE_FATAL);
529                 tar->entry_padding = 0;
530                 *buff = NULL;
531                 *size = 0;
532                 *offset = tar->entry_offset;
533                 return (ARCHIVE_EOF);
534         }
535 }
536
537 static int
538 archive_read_format_tar_skip(struct archive_read *a)
539 {
540         off_t bytes_skipped;
541         struct tar* tar;
542         struct sparse_block *p;
543
544         tar = (struct tar *)*(a->pformat_data);
545
546         /*
547          * Compression layer skip functions are required to either skip the
548          * length requested or fail, so we can rely upon the entire entry
549          * plus padding being skipped.
550          */
551         bytes_skipped = (a->compression_skip)(a, tar->entry_bytes_remaining +
552             tar->entry_padding);
553         if (bytes_skipped < 0)
554                 return (ARCHIVE_FATAL);
555
556         tar->entry_bytes_remaining = 0;
557         tar->entry_padding = 0;
558
559         /* Free the sparse list. */
560         while (tar->sparse_list != NULL) {
561                 p = tar->sparse_list;
562                 tar->sparse_list = p->next;
563                 free(p);
564         }
565
566         return (ARCHIVE_OK);
567 }
568
569 /*
570  * This function recursively interprets all of the headers associated
571  * with a single entry.
572  */
573 static int
574 tar_read_header(struct archive_read *a, struct tar *tar,
575     struct archive_entry *entry, struct stat *st)
576 {
577         ssize_t bytes;
578         int err;
579         const void *h;
580         const struct archive_entry_header_ustar *header;
581
582         /* Read 512-byte header record */
583         bytes = (a->compression_read_ahead)(a, &h, 512);
584         if (bytes < 512) {
585                 /*
586                  * If we're here, it's becase the _bid function accepted
587                  * this file.  So just call a short read end-of-archive
588                  * and be done with it.
589                  */
590                 return (ARCHIVE_EOF);
591         }
592         (a->compression_read_consume)(a, 512);
593
594         /* Check for end-of-archive mark. */
595         if (((*(const char *)h)==0) && archive_block_is_null((const unsigned char *)h)) {
596                 /* Try to consume a second all-null record, as well. */
597                 bytes = (a->compression_read_ahead)(a, &h, 512);
598                 if (bytes > 0)
599                         (a->compression_read_consume)(a, bytes);
600                 archive_set_error(&a->archive, 0, NULL);
601                 return (ARCHIVE_EOF);
602         }
603
604         /*
605          * Note: If the checksum fails and we return ARCHIVE_RETRY,
606          * then the client is likely to just retry.  This is a very
607          * crude way to search for the next valid header!
608          *
609          * TODO: Improve this by implementing a real header scan.
610          */
611         if (!checksum(a, h)) {
612                 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
613                 return (ARCHIVE_RETRY); /* Retryable: Invalid header */
614         }
615
616         if (++tar->header_recursion_depth > 32) {
617                 archive_set_error(&a->archive, EINVAL, "Too many special headers");
618                 return (ARCHIVE_WARN);
619         }
620
621         /* Determine the format variant. */
622         header = (const struct archive_entry_header_ustar *)h;
623         switch(header->typeflag[0]) {
624         case 'A': /* Solaris tar ACL */
625                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
626                 a->archive.archive_format_name = "Solaris tar";
627                 err = header_Solaris_ACL(a, tar, entry, st, h);
628                 break;
629         case 'g': /* POSIX-standard 'g' header. */
630                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
631                 a->archive.archive_format_name = "POSIX pax interchange format";
632                 err = header_pax_global(a, tar, entry, st, h);
633                 break;
634         case 'K': /* Long link name (GNU tar, others) */
635                 err = header_longlink(a, tar, entry, st, h);
636                 break;
637         case 'L': /* Long filename (GNU tar, others) */
638                 err = header_longname(a, tar, entry, st, h);
639                 break;
640         case 'V': /* GNU volume header */
641                 err = header_volume(a, tar, entry, st, h);
642                 break;
643         case 'X': /* Used by SUN tar; same as 'x'. */
644                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
645                 a->archive.archive_format_name =
646                     "POSIX pax interchange format (Sun variant)";
647                 err = header_pax_extensions(a, tar, entry, st, h);
648                 break;
649         case 'x': /* POSIX-standard 'x' header. */
650                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
651                 a->archive.archive_format_name = "POSIX pax interchange format";
652                 err = header_pax_extensions(a, tar, entry, st, h);
653                 break;
654         default:
655                 if (memcmp(header->magic, "ustar  \0", 8) == 0) {
656                         a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
657                         a->archive.archive_format_name = "GNU tar format";
658                         err = header_gnutar(a, tar, entry, st, h);
659                 } else if (memcmp(header->magic, "ustar", 5) == 0) {
660                         if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
661                                 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
662                                 a->archive.archive_format_name = "POSIX ustar format";
663                         }
664                         err = header_ustar(a, tar, entry, st, h);
665                 } else {
666                         a->archive.archive_format = ARCHIVE_FORMAT_TAR;
667                         a->archive.archive_format_name = "tar (non-POSIX)";
668                         err = header_old_tar(a, tar, entry, st, h);
669                 }
670         }
671         --tar->header_recursion_depth;
672         return (err);
673 }
674
675 /*
676  * Return true if block checksum is correct.
677  */
678 static int
679 checksum(struct archive_read *a, const void *h)
680 {
681         const unsigned char *bytes;
682         const struct archive_entry_header_ustar *header;
683         int check, i, sum;
684
685         (void)a; /* UNUSED */
686         bytes = (const unsigned char *)h;
687         header = (const struct archive_entry_header_ustar *)h;
688
689         /*
690          * Test the checksum.  Note that POSIX specifies _unsigned_
691          * bytes for this calculation.
692          */
693         sum = tar_atol(header->checksum, sizeof(header->checksum));
694         check = 0;
695         for (i = 0; i < 148; i++)
696                 check += (unsigned char)bytes[i];
697         for (; i < 156; i++)
698                 check += 32;
699         for (; i < 512; i++)
700                 check += (unsigned char)bytes[i];
701         if (sum == check)
702                 return (1);
703
704         /*
705          * Repeat test with _signed_ bytes, just in case this archive
706          * was created by an old BSD, Solaris, or HP-UX tar with a
707          * broken checksum calculation.
708          */
709         check = 0;
710         for (i = 0; i < 148; i++)
711                 check += (signed char)bytes[i];
712         for (; i < 156; i++)
713                 check += 32;
714         for (; i < 512; i++)
715                 check += (signed char)bytes[i];
716         if (sum == check)
717                 return (1);
718
719         return (0);
720 }
721
722 /*
723  * Return true if this block contains only nulls.
724  */
725 static int
726 archive_block_is_null(const unsigned char *p)
727 {
728         unsigned i;
729
730         for (i = 0; i < ARCHIVE_BYTES_PER_RECORD / sizeof(*p); i++)
731                 if (*p++)
732                         return (0);
733         return (1);
734 }
735
736 /*
737  * Interpret 'A' Solaris ACL header
738  */
739 static int
740 header_Solaris_ACL(struct archive_read *a, struct tar *tar,
741     struct archive_entry *entry, struct stat *st, const void *h)
742 {
743         int err, err2;
744         char *p;
745         wchar_t *wp;
746
747         err = read_body_to_string(a, tar, &(tar->acl_text), h);
748         err2 = tar_read_header(a, tar, entry, st);
749         err = err_combine(err, err2);
750
751         /* XXX Ensure p doesn't overrun acl_text */
752
753         /* Skip leading octal number. */
754         /* XXX TODO: Parse the octal number and sanity-check it. */
755         p = tar->acl_text.s;
756         while (*p != '\0')
757                 p++;
758         p++;
759
760         wp = (wchar_t *)malloc((strlen(p) + 1) * sizeof(wchar_t));
761         if (wp != NULL) {
762                 utf8_decode(wp, p, strlen(p));
763                 err2 = __archive_entry_acl_parse_w(entry, wp,
764                     ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
765                 err = err_combine(err, err2);
766                 free(wp);
767         }
768
769         return (err);
770 }
771
772 /*
773  * Interpret 'K' long linkname header.
774  */
775 static int
776 header_longlink(struct archive_read *a, struct tar *tar,
777     struct archive_entry *entry, struct stat *st, const void *h)
778 {
779         int err, err2;
780
781         err = read_body_to_string(a, tar, &(tar->longlink), h);
782         err2 = tar_read_header(a, tar, entry, st);
783         if (err == ARCHIVE_OK && err2 == ARCHIVE_OK) {
784                 /* Set symlink if symlink already set, else hardlink. */
785                 archive_entry_set_link(entry, tar->longlink.s);
786         }
787         return (err_combine(err, err2));
788 }
789
790 /*
791  * Interpret 'L' long filename header.
792  */
793 static int
794 header_longname(struct archive_read *a, struct tar *tar,
795     struct archive_entry *entry, struct stat *st, const void *h)
796 {
797         int err, err2;
798
799         err = read_body_to_string(a, tar, &(tar->longname), h);
800         /* Read and parse "real" header, then override name. */
801         err2 = tar_read_header(a, tar, entry, st);
802         if (err == ARCHIVE_OK && err2 == ARCHIVE_OK)
803                 archive_entry_set_pathname(entry, tar->longname.s);
804         return (err_combine(err, err2));
805 }
806
807
808 /*
809  * Interpret 'V' GNU tar volume header.
810  */
811 static int
812 header_volume(struct archive_read *a, struct tar *tar,
813     struct archive_entry *entry, struct stat *st, const void *h)
814 {
815         (void)h;
816
817         /* Just skip this and read the next header. */
818         return (tar_read_header(a, tar, entry, st));
819 }
820
821 /*
822  * Read body of an archive entry into an archive_string object.
823  */
824 static int
825 read_body_to_string(struct archive_read *a, struct tar *tar,
826     struct archive_string *as, const void *h)
827 {
828         off_t size, padded_size;
829         ssize_t bytes_read, bytes_to_copy;
830         const struct archive_entry_header_ustar *header;
831         const void *src;
832         char *dest;
833
834         (void)tar; /* UNUSED */
835         header = (const struct archive_entry_header_ustar *)h;
836         size  = tar_atol(header->size, sizeof(header->size));
837
838         /* Read the body into the string. */
839         archive_string_ensure(as, size+1);
840         padded_size = (size + 511) & ~ 511;
841         dest = as->s;
842         while (padded_size > 0) {
843                 bytes_read = (a->compression_read_ahead)(a, &src, padded_size);
844                 if (bytes_read < 0)
845                         return (ARCHIVE_FATAL);
846                 if (bytes_read > padded_size)
847                         bytes_read = padded_size;
848                 (a->compression_read_consume)(a, bytes_read);
849                 bytes_to_copy = bytes_read;
850                 if ((off_t)bytes_to_copy > size)
851                         bytes_to_copy = (ssize_t)size;
852                 memcpy(dest, src, bytes_to_copy);
853                 dest += bytes_to_copy;
854                 size -= bytes_to_copy;
855                 padded_size -= bytes_read;
856         }
857         *dest = '\0';
858         return (ARCHIVE_OK);
859 }
860
861 /*
862  * Parse out common header elements.
863  *
864  * This would be the same as header_old_tar, except that the
865  * filename is handled slightly differently for old and POSIX
866  * entries  (POSIX entries support a 'prefix').  This factoring
867  * allows header_old_tar and header_ustar
868  * to handle filenames differently, while still putting most of the
869  * common parsing into one place.
870  */
871 static int
872 header_common(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
873     struct stat *st, const void *h)
874 {
875         const struct archive_entry_header_ustar *header;
876         char    tartype;
877
878         (void)a; /* UNUSED */
879
880         header = (const struct archive_entry_header_ustar *)h;
881         if (header->linkname[0])
882                 archive_strncpy(&(tar->entry_linkname), header->linkname,
883                     sizeof(header->linkname));
884         else
885                 archive_string_empty(&(tar->entry_linkname));
886
887         /* Parse out the numeric fields (all are octal) */
888         st->st_mode  = tar_atol(header->mode, sizeof(header->mode));
889         st->st_uid   = tar_atol(header->uid, sizeof(header->uid));
890         st->st_gid   = tar_atol(header->gid, sizeof(header->gid));
891         st->st_size  = tar_atol(header->size, sizeof(header->size));
892         st->st_mtime = tar_atol(header->mtime, sizeof(header->mtime));
893
894         /* Handle the tar type flag appropriately. */
895         tartype = header->typeflag[0];
896         st->st_mode &= ~S_IFMT;
897
898         switch (tartype) {
899         case '1': /* Hard link */
900                 archive_entry_set_hardlink(entry, tar->entry_linkname.s);
901                 /*
902                  * The following may seem odd, but: Technically, tar
903                  * does not store the file type for a "hard link"
904                  * entry, only the fact that it is a hard link.  So, I
905                  * leave the type zero normally.  But, pax interchange
906                  * format allows hard links to have data, which
907                  * implies that the underlying entry is a regular
908                  * file.
909                  */
910                 if (st->st_size > 0)
911                         st->st_mode |= S_IFREG;
912
913                 /*
914                  * A tricky point: Traditionally, tar readers have
915                  * ignored the size field when reading hardlink
916                  * entries, and some writers put non-zero sizes even
917                  * though the body is empty.  POSIX.1-2001 broke with
918                  * this tradition by permitting hardlink entries to
919                  * store valid bodies in pax interchange format, but
920                  * not in ustar format.  Since there is no hard and
921                  * fast way to distinguish pax interchange from
922                  * earlier archives (the 'x' and 'g' entries are
923                  * optional, after all), we need a heuristic.  Here, I
924                  * use the bid function to test whether or not there's
925                  * a valid header following.  Of course, if we know
926                  * this is pax interchange format, then we must obey
927                  * the size.
928                  *
929                  * This heuristic will only fail for a pax interchange
930                  * archive that is storing hardlink bodies, no pax
931                  * extended attribute entries have yet occurred, and
932                  * we encounter a hardlink entry for a file that is
933                  * itself an uncompressed tar archive.
934                  */
935                 if (st->st_size > 0  &&
936                     a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE  &&
937                     archive_read_format_tar_bid(a) > 50)
938                         st->st_size = 0;
939                 break;
940         case '2': /* Symlink */
941                 st->st_mode |= S_IFLNK;
942                 st->st_size = 0;
943                 archive_entry_set_symlink(entry, tar->entry_linkname.s);
944                 break;
945         case '3': /* Character device */
946                 st->st_mode |= S_IFCHR;
947                 st->st_size = 0;
948                 break;
949         case '4': /* Block device */
950                 st->st_mode |= S_IFBLK;
951                 st->st_size = 0;
952                 break;
953         case '5': /* Dir */
954                 st->st_mode |= S_IFDIR;
955                 st->st_size = 0;
956                 break;
957         case '6': /* FIFO device */
958                 st->st_mode |= S_IFIFO;
959                 st->st_size = 0;
960                 break;
961         case 'D': /* GNU incremental directory type */
962                 /*
963                  * No special handling is actually required here.
964                  * It might be nice someday to preprocess the file list and
965                  * provide it to the client, though.
966                  */
967                 st->st_mode |= S_IFDIR;
968                 break;
969         case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
970                 /*
971                  * As far as I can tell, this is just like a regular file
972                  * entry, except that the contents should be _appended_ to
973                  * the indicated file at the indicated offset.  This may
974                  * require some API work to fully support.
975                  */
976                 break;
977         case 'N': /* Old GNU "long filename" entry. */
978                 /* The body of this entry is a script for renaming
979                  * previously-extracted entries.  Ugh.  It will never
980                  * be supported by libarchive. */
981                 st->st_mode |= S_IFREG;
982                 break;
983         case 'S': /* GNU sparse files */
984                 /*
985                  * Sparse files are really just regular files with
986                  * sparse information in the extended area.
987                  */
988                 /* FALL THROUGH */
989         default: /* Regular file  and non-standard types */
990                 /*
991                  * Per POSIX: non-recognized types should always be
992                  * treated as regular files.
993                  */
994                 st->st_mode |= S_IFREG;
995                 break;
996         }
997         return (0);
998 }
999
1000 /*
1001  * Parse out header elements for "old-style" tar archives.
1002  */
1003 static int
1004 header_old_tar(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1005     struct stat *st, const void *h)
1006 {
1007         const struct archive_entry_header_ustar *header;
1008
1009         /* Copy filename over (to ensure null termination). */
1010         header = (const struct archive_entry_header_ustar *)h;
1011         archive_strncpy(&(tar->entry_name), header->name, sizeof(header->name));
1012         archive_entry_set_pathname(entry, tar->entry_name.s);
1013
1014         /* Grab rest of common fields */
1015         header_common(a, tar, entry, st, h);
1016
1017         tar->entry_bytes_remaining = st->st_size;
1018         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1019         return (0);
1020 }
1021
1022 /*
1023  * Parse a file header for a pax extended archive entry.
1024  */
1025 static int
1026 header_pax_global(struct archive_read *a, struct tar *tar,
1027     struct archive_entry *entry, struct stat *st, const void *h)
1028 {
1029         int err, err2;
1030
1031         err = read_body_to_string(a, tar, &(tar->pax_global), h);
1032         err2 = tar_read_header(a, tar, entry, st);
1033         return (err_combine(err, err2));
1034 }
1035
1036 static int
1037 header_pax_extensions(struct archive_read *a, struct tar *tar,
1038     struct archive_entry *entry, struct stat *st, const void *h)
1039 {
1040         int err, err2;
1041
1042         read_body_to_string(a, tar, &(tar->pax_header), h);
1043
1044         /* Parse the next header. */
1045         err = tar_read_header(a, tar, entry, st);
1046
1047         /*
1048          * TODO: Parse global/default options into 'entry' struct here
1049          * before handling file-specific options.
1050          *
1051          * This design (parse standard header, then overwrite with pax
1052          * extended attribute data) usually works well, but isn't ideal;
1053          * it would be better to parse the pax extended attributes first
1054          * and then skip any fields in the standard header that were
1055          * defined in the pax header.
1056          */
1057         err2 = pax_header(a, tar, entry, st, tar->pax_header.s);
1058         err =  err_combine(err, err2);
1059         tar->entry_bytes_remaining = st->st_size;
1060         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1061         return (err);
1062 }
1063
1064
1065 /*
1066  * Parse a file header for a Posix "ustar" archive entry.  This also
1067  * handles "pax" or "extended ustar" entries.
1068  */
1069 static int
1070 header_ustar(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1071     struct stat *st, const void *h)
1072 {
1073         const struct archive_entry_header_ustar *header;
1074         struct archive_string *as;
1075
1076         header = (const struct archive_entry_header_ustar *)h;
1077
1078         /* Copy name into an internal buffer to ensure null-termination. */
1079         as = &(tar->entry_name);
1080         if (header->prefix[0]) {
1081                 archive_strncpy(as, header->prefix, sizeof(header->prefix));
1082                 if (as->s[archive_strlen(as) - 1] != '/')
1083                         archive_strappend_char(as, '/');
1084                 archive_strncat(as, header->name, sizeof(header->name));
1085         } else
1086                 archive_strncpy(as, header->name, sizeof(header->name));
1087
1088         archive_entry_set_pathname(entry, as->s);
1089
1090         /* Handle rest of common fields. */
1091         header_common(a, tar, entry, st, h);
1092
1093         /* Handle POSIX ustar fields. */
1094         archive_strncpy(&(tar->entry_uname), header->uname,
1095             sizeof(header->uname));
1096         archive_entry_set_uname(entry, tar->entry_uname.s);
1097
1098         archive_strncpy(&(tar->entry_gname), header->gname,
1099             sizeof(header->gname));
1100         archive_entry_set_gname(entry, tar->entry_gname.s);
1101
1102         /* Parse out device numbers only for char and block specials. */
1103         if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1104                 st->st_rdev = makedev(
1105                     tar_atol(header->rdevmajor, sizeof(header->rdevmajor)),
1106                     tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1107         }
1108
1109         tar->entry_bytes_remaining = st->st_size;
1110         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1111
1112         return (0);
1113 }
1114
1115
1116 /*
1117  * Parse the pax extended attributes record.
1118  *
1119  * Returns non-zero if there's an error in the data.
1120  */
1121 static int
1122 pax_header(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1123     struct stat *st, char *attr)
1124 {
1125         size_t attr_length, l, line_length;
1126         char *line, *p;
1127         wchar_t *key, *wp, *value;
1128         int err, err2;
1129
1130         attr_length = strlen(attr);
1131         err = ARCHIVE_OK;
1132         while (attr_length > 0) {
1133                 /* Parse decimal length field at start of line. */
1134                 line_length = 0;
1135                 l = attr_length;
1136                 line = p = attr; /* Record start of line. */
1137                 while (l>0) {
1138                         if (*p == ' ') {
1139                                 p++;
1140                                 l--;
1141                                 break;
1142                         }
1143                         if (*p < '0' || *p > '9')
1144                                 return (-1);
1145                         line_length *= 10;
1146                         line_length += *p - '0';
1147                         if (line_length > 999999) {
1148                                 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1149                                     "Rejecting pax extended attribute > 1MB");
1150                                 return (ARCHIVE_WARN);
1151                         }
1152                         p++;
1153                         l--;
1154                 }
1155
1156                 if (line_length > attr_length)
1157                         return (0);
1158
1159                 /* Ensure pax_entry buffer is big enough. */
1160                 if (tar->pax_entry_length <= line_length) {
1161                         wchar_t *old_entry = tar->pax_entry;
1162
1163                         if (tar->pax_entry_length <= 0)
1164                                 tar->pax_entry_length = 1024;
1165                         while (tar->pax_entry_length <= line_length + 1)
1166                                 tar->pax_entry_length *= 2;
1167
1168                         old_entry = tar->pax_entry;
1169                         tar->pax_entry = (wchar_t *)realloc(tar->pax_entry,
1170                             tar->pax_entry_length * sizeof(wchar_t));
1171                         if (tar->pax_entry == NULL) {
1172                                 free(old_entry);
1173                                 archive_set_error(&a->archive, ENOMEM,
1174                                         "No memory");
1175                                 return (ARCHIVE_FATAL);
1176                         }
1177                 }
1178
1179                 /* Decode UTF-8 to wchar_t, null-terminate result. */
1180                 if (utf8_decode(tar->pax_entry, p,
1181                         line_length - (p - attr) - 1)) {
1182                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1183                            "Invalid UTF8 character in pax extended attribute");
1184                         err = err_combine(err, ARCHIVE_WARN);
1185                 }
1186
1187                 /* Null-terminate 'key' value. */
1188                 wp = key = tar->pax_entry;
1189                 if (key[0] == L'=')
1190                         return (-1);
1191                 while (*wp && *wp != L'=')
1192                         ++wp;
1193                 if (*wp == L'\0' || wp == NULL) {
1194                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1195                             "Invalid pax extended attributes");
1196                         return (ARCHIVE_WARN);
1197                 }
1198                 *wp = 0;
1199
1200                 /* Identify null-terminated 'value' portion. */
1201                 value = wp + 1;
1202
1203                 /* Identify this attribute and set it in the entry. */
1204                 err2 = pax_attribute(entry, st, key, value);
1205                 err = err_combine(err, err2);
1206
1207                 /* Skip to next line */
1208                 attr += line_length;
1209                 attr_length -= line_length;
1210         }
1211         return (err);
1212 }
1213
1214 static int
1215 pax_attribute_xattr(struct archive_entry *entry,
1216         wchar_t *name, wchar_t *value)
1217 {
1218         char *name_decoded, *name_narrow;
1219         void *value_decoded;
1220         size_t value_len;
1221
1222         if (wcslen(name) < 18 || (wcsncmp(name, L"LIBARCHIVE.xattr.", 17)) != 0)
1223                 return 3;
1224
1225         name += 17;
1226
1227         /* URL-decode name */
1228         name_narrow = wide_to_narrow(name);
1229         if (name_narrow == NULL)
1230                 return 2;
1231         name_decoded = url_decode(name_narrow);
1232         free(name_narrow);
1233         if (name_decoded == NULL)
1234                 return 2;
1235
1236         /* Base-64 decode value */
1237         value_decoded = base64_decode(value, wcslen(value), &value_len);
1238         if (value_decoded == NULL) {
1239                 free(name_decoded);
1240                 return 1;
1241         }
1242
1243         archive_entry_xattr_add_entry(entry, name_decoded,
1244                 value_decoded, value_len);
1245
1246         free(name_decoded);
1247         free(value_decoded);
1248         return 0;
1249 }
1250
1251 /*
1252  * Parse a single key=value attribute.  key/value pointers are
1253  * assumed to point into reasonably long-lived storage.
1254  *
1255  * Note that POSIX reserves all-lowercase keywords.  Vendor-specific
1256  * extensions should always have keywords of the form "VENDOR.attribute"
1257  * In particular, it's quite feasible to support many different
1258  * vendor extensions here.  I'm using "LIBARCHIVE" for extensions
1259  * unique to this library (currently, there are none).
1260  *
1261  * Investigate other vendor-specific extensions, as well and see if
1262  * any of them look useful.
1263  */
1264 static int
1265 pax_attribute(struct archive_entry *entry, struct stat *st,
1266     wchar_t *key, wchar_t *value)
1267 {
1268         int64_t s;
1269         long n;
1270
1271         switch (key[0]) {
1272         case 'L':
1273                 /* Our extensions */
1274 /* TODO: Handle arbitrary extended attributes... */
1275 /*
1276                 if (strcmp(key, "LIBARCHIVE.xxxxxxx")==0)
1277                         archive_entry_set_xxxxxx(entry, value);
1278 */
1279                 if (wcsncmp(key, L"LIBARCHIVE.xattr.", 17)==0)
1280                         pax_attribute_xattr(entry, key, value);
1281                 break;
1282         case 'S':
1283                 /* We support some keys used by the "star" archiver */
1284                 if (wcscmp(key, L"SCHILY.acl.access")==0)
1285                         __archive_entry_acl_parse_w(entry, value,
1286                             ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
1287                 else if (wcscmp(key, L"SCHILY.acl.default")==0)
1288                         __archive_entry_acl_parse_w(entry, value,
1289                             ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1290                 else if (wcscmp(key, L"SCHILY.devmajor")==0)
1291                         st->st_rdev = makedev(tar_atol10(value, wcslen(value)),
1292                             minor(st->st_rdev));
1293                 else if (wcscmp(key, L"SCHILY.devminor")==0)
1294                         st->st_rdev = makedev(major(st->st_rdev),
1295                             tar_atol10(value, wcslen(value)));
1296                 else if (wcscmp(key, L"SCHILY.fflags")==0)
1297                         archive_entry_copy_fflags_text_w(entry, value);
1298                 else if (wcscmp(key, L"SCHILY.dev")==0)
1299                         st->st_dev = tar_atol10(value, wcslen(value));
1300                 else if (wcscmp(key, L"SCHILY.ino")==0)
1301                         st->st_ino = tar_atol10(value, wcslen(value));
1302                 else if (wcscmp(key, L"SCHILY.nlink")==0)
1303                         st->st_nlink = tar_atol10(value, wcslen(value));
1304                 break;
1305         case 'a':
1306                 if (wcscmp(key, L"atime")==0) {
1307                         pax_time(value, &s, &n);
1308                         st->st_atime = s;
1309                         ARCHIVE_STAT_SET_ATIME_NANOS(st, n);
1310                 }
1311                 break;
1312         case 'c':
1313                 if (wcscmp(key, L"ctime")==0) {
1314                         pax_time(value, &s, &n);
1315                         st->st_ctime = s;
1316                         ARCHIVE_STAT_SET_CTIME_NANOS(st, n);
1317                 } else if (wcscmp(key, L"charset")==0) {
1318                         /* TODO: Publish charset information in entry. */
1319                 } else if (wcscmp(key, L"comment")==0) {
1320                         /* TODO: Publish comment in entry. */
1321                 }
1322                 break;
1323         case 'g':
1324                 if (wcscmp(key, L"gid")==0)
1325                         st->st_gid = tar_atol10(value, wcslen(value));
1326                 else if (wcscmp(key, L"gname")==0)
1327                         archive_entry_copy_gname_w(entry, value);
1328                 break;
1329         case 'l':
1330                 /* pax interchange doesn't distinguish hardlink vs. symlink. */
1331                 if (wcscmp(key, L"linkpath")==0) {
1332                         if (archive_entry_hardlink(entry))
1333                                 archive_entry_copy_hardlink_w(entry, value);
1334                         else
1335                                 archive_entry_copy_symlink_w(entry, value);
1336                 }
1337                 break;
1338         case 'm':
1339                 if (wcscmp(key, L"mtime")==0) {
1340                         pax_time(value, &s, &n);
1341                         st->st_mtime = s;
1342                         ARCHIVE_STAT_SET_MTIME_NANOS(st, n);
1343                 }
1344                 break;
1345         case 'p':
1346                 if (wcscmp(key, L"path")==0)
1347                         archive_entry_copy_pathname_w(entry, value);
1348                 break;
1349         case 'r':
1350                 /* POSIX has reserved 'realtime.*' */
1351                 break;
1352         case 's':
1353                 /* POSIX has reserved 'security.*' */
1354                 /* Someday: if (wcscmp(key, L"security.acl")==0) { ... } */
1355                 if (wcscmp(key, L"size")==0)
1356                         st->st_size = tar_atol10(value, wcslen(value));
1357                 break;
1358         case 'u':
1359                 if (wcscmp(key, L"uid")==0)
1360                         st->st_uid = tar_atol10(value, wcslen(value));
1361                 else if (wcscmp(key, L"uname")==0)
1362                         archive_entry_copy_uname_w(entry, value);
1363                 break;
1364         }
1365         return (0);
1366 }
1367
1368
1369
1370 /*
1371  * parse a decimal time value, which may include a fractional portion
1372  */
1373 static void
1374 pax_time(const wchar_t *p, int64_t *ps, long *pn)
1375 {
1376         char digit;
1377         int64_t s;
1378         unsigned long l;
1379         int sign;
1380         int64_t limit, last_digit_limit;
1381
1382         limit = max_int64 / 10;
1383         last_digit_limit = max_int64 % 10;
1384
1385         s = 0;
1386         sign = 1;
1387         if (*p == '-') {
1388                 sign = -1;
1389                 p++;
1390         }
1391         while (*p >= '0' && *p <= '9') {
1392                 digit = *p - '0';
1393                 if (s > limit ||
1394                     (s == limit && digit > last_digit_limit)) {
1395                         s = max_uint64;
1396                         break;
1397                 }
1398                 s = (s * 10) + digit;
1399                 ++p;
1400         }
1401
1402         *ps = s * sign;
1403
1404         /* Calculate nanoseconds. */
1405         *pn = 0;
1406
1407         if (*p != '.')
1408                 return;
1409
1410         l = 100000000UL;
1411         do {
1412                 ++p;
1413                 if (*p >= '0' && *p <= '9')
1414                         *pn += (*p - '0') * l;
1415                 else
1416                         break;
1417         } while (l /= 10);
1418 }
1419
1420 /*
1421  * Parse GNU tar header
1422  */
1423 static int
1424 header_gnutar(struct archive_read *a, struct tar *tar, struct archive_entry *entry,
1425     struct stat *st, const void *h)
1426 {
1427         const struct archive_entry_header_gnutar *header;
1428
1429         (void)a;
1430
1431         /*
1432          * GNU header is like POSIX ustar, except 'prefix' is
1433          * replaced with some other fields. This also means the
1434          * filename is stored as in old-style archives.
1435          */
1436
1437         /* Grab fields common to all tar variants. */
1438         header_common(a, tar, entry, st, h);
1439
1440         /* Copy filename over (to ensure null termination). */
1441         header = (const struct archive_entry_header_gnutar *)h;
1442         archive_strncpy(&(tar->entry_name), header->name,
1443             sizeof(header->name));
1444         archive_entry_set_pathname(entry, tar->entry_name.s);
1445
1446         /* Fields common to ustar and GNU */
1447         /* XXX Can the following be factored out since it's common
1448          * to ustar and gnu tar?  Is it okay to move it down into
1449          * header_common, perhaps?  */
1450         archive_strncpy(&(tar->entry_uname),
1451             header->uname, sizeof(header->uname));
1452         archive_entry_set_uname(entry, tar->entry_uname.s);
1453
1454         archive_strncpy(&(tar->entry_gname),
1455             header->gname, sizeof(header->gname));
1456         archive_entry_set_gname(entry, tar->entry_gname.s);
1457
1458         /* Parse out device numbers only for char and block specials */
1459         if (header->typeflag[0] == '3' || header->typeflag[0] == '4')
1460                 st->st_rdev = makedev (
1461                     tar_atol(header->rdevmajor, sizeof(header->rdevmajor)),
1462                     tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1463         else
1464                 st->st_rdev = 0;
1465
1466         tar->entry_bytes_remaining = st->st_size;
1467         tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1468
1469         /* Grab GNU-specific fields. */
1470         st->st_atime = tar_atol(header->atime, sizeof(header->atime));
1471         st->st_ctime = tar_atol(header->ctime, sizeof(header->ctime));
1472         if (header->realsize[0] != 0) {
1473                 st->st_size = tar_atol(header->realsize,
1474                     sizeof(header->realsize));
1475         }
1476
1477         if (header->sparse[0].offset[0] != 0) {
1478                 gnu_read_sparse_data(a, tar, header);
1479         } else {
1480                 if (header->isextended[0] != 0) {
1481                         /* XXX WTF? XXX */
1482                 }
1483         }
1484
1485         return (0);
1486 }
1487
1488 static int
1489 gnu_read_sparse_data(struct archive_read *a, struct tar *tar,
1490     const struct archive_entry_header_gnutar *header)
1491 {
1492         ssize_t bytes_read;
1493         const void *data;
1494         struct extended {
1495                 struct gnu_sparse sparse[21];
1496                 char    isextended[1];
1497                 char    padding[7];
1498         };
1499         const struct extended *ext;
1500
1501         gnu_parse_sparse_data(a, tar, header->sparse, 4);
1502         if (header->isextended[0] == 0)
1503                 return (ARCHIVE_OK);
1504
1505         do {
1506                 bytes_read = (a->compression_read_ahead)(a, &data, 512);
1507                 if (bytes_read < 0)
1508                         return (ARCHIVE_FATAL);
1509                 if (bytes_read < 512) {
1510                         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1511                             "Truncated tar archive "
1512                             "detected while reading sparse file data");
1513                         return (ARCHIVE_FATAL);
1514                 }
1515                 (a->compression_read_consume)(a, 512);
1516                 ext = (const struct extended *)data;
1517                 gnu_parse_sparse_data(a, tar, ext->sparse, 21);
1518         } while (ext->isextended[0] != 0);
1519         if (tar->sparse_list != NULL)
1520                 tar->entry_offset = tar->sparse_list->offset;
1521         return (ARCHIVE_OK);
1522 }
1523
1524 static void
1525 gnu_parse_sparse_data(struct archive_read *a, struct tar *tar,
1526     const struct gnu_sparse *sparse, int length)
1527 {
1528         struct sparse_block *last;
1529         struct sparse_block *p;
1530
1531         (void)a; /* UNUSED */
1532
1533         last = tar->sparse_list;
1534         while (last != NULL && last->next != NULL)
1535                 last = last->next;
1536
1537         while (length > 0 && sparse->offset[0] != 0) {
1538                 p = (struct sparse_block *)malloc(sizeof(*p));
1539                 if (p == NULL)
1540                         __archive_errx(1, "Out of memory");
1541                 memset(p, 0, sizeof(*p));
1542                 if (last != NULL)
1543                         last->next = p;
1544                 else
1545                         tar->sparse_list = p;
1546                 last = p;
1547                 p->offset = tar_atol(sparse->offset, sizeof(sparse->offset));
1548                 p->remaining =
1549                     tar_atol(sparse->numbytes, sizeof(sparse->numbytes));
1550                 sparse++;
1551                 length--;
1552         }
1553 }
1554
1555 /*-
1556  * Convert text->integer.
1557  *
1558  * Traditional tar formats (including POSIX) specify base-8 for
1559  * all of the standard numeric fields.  This is a significant limitation
1560  * in practice:
1561  *   = file size is limited to 8GB
1562  *   = rdevmajor and rdevminor are limited to 21 bits
1563  *   = uid/gid are limited to 21 bits
1564  *
1565  * There are two workarounds for this:
1566  *   = pax extended headers, which use variable-length string fields
1567  *   = GNU tar and STAR both allow either base-8 or base-256 in
1568  *      most fields.  The high bit is set to indicate base-256.
1569  *
1570  * On read, this implementation supports both extensions.
1571  */
1572 static int64_t
1573 tar_atol(const char *p, unsigned char_cnt)
1574 {
1575         /*
1576          * Technically, GNU tar considers a field to be in base-256
1577          * only if the first byte is 0xff or 0x80.
1578          */
1579         if (*p & 0x80)
1580                 return (tar_atol256(p, char_cnt));
1581         return (tar_atol8(p, char_cnt));
1582 }
1583
1584 /*
1585  * Note that this implementation does not (and should not!) obey
1586  * locale settings; you cannot simply substitute strtol here, since
1587  * it does obey locale.
1588  */
1589 static int64_t
1590 tar_atol8(const char *p, unsigned char_cnt)
1591 {
1592         int64_t l, limit, last_digit_limit;
1593         int digit, sign, base;
1594
1595         base = 8;
1596         limit = max_int64 / base;
1597         last_digit_limit = max_int64 % base;
1598
1599         while (*p == ' ' || *p == '\t')
1600                 p++;
1601         if (*p == '-') {
1602                 sign = -1;
1603                 p++;
1604         } else
1605                 sign = 1;
1606
1607         l = 0;
1608         digit = *p - '0';
1609         while (digit >= 0 && digit < base  && char_cnt-- > 0) {
1610                 if (l>limit || (l == limit && digit > last_digit_limit)) {
1611                         l = max_uint64; /* Truncate on overflow. */
1612                         break;
1613                 }
1614                 l = (l * base) + digit;
1615                 digit = *++p - '0';
1616         }
1617         return (sign < 0) ? -l : l;
1618 }
1619
1620
1621 /*
1622  * Note that this implementation does not (and should not!) obey
1623  * locale settings; you cannot simply substitute strtol here, since
1624  * it does obey locale.
1625  */
1626 static int64_t
1627 tar_atol10(const wchar_t *p, unsigned char_cnt)
1628 {
1629         int64_t l, limit, last_digit_limit;
1630         int base, digit, sign;
1631
1632         base = 10;
1633         limit = max_int64 / base;
1634         last_digit_limit = max_int64 % base;
1635
1636         while (*p == ' ' || *p == '\t')
1637                 p++;
1638         if (*p == '-') {
1639                 sign = -1;
1640                 p++;
1641         } else
1642                 sign = 1;
1643
1644         l = 0;
1645         digit = *p - '0';
1646         while (digit >= 0 && digit < base  && char_cnt-- > 0) {
1647                 if (l > limit || (l == limit && digit > last_digit_limit)) {
1648                         l = max_uint64; /* Truncate on overflow. */
1649                         break;
1650                 }
1651                 l = (l * base) + digit;
1652                 digit = *++p - '0';
1653         }
1654         return (sign < 0) ? -l : l;
1655 }
1656
1657 /*
1658  * Parse a base-256 integer.  This is just a straight signed binary
1659  * value in big-endian order, except that the high-order bit is
1660  * ignored.  Remember that "int64_t" may or may not be exactly 64
1661  * bits; the implementation here tries to avoid making any assumptions
1662  * about the actual size of an int64_t.  It does assume we're using
1663  * twos-complement arithmetic, though.
1664  */
1665 static int64_t
1666 tar_atol256(const char *_p, unsigned char_cnt)
1667 {
1668         int64_t l, upper_limit, lower_limit;
1669         const unsigned char *p = (const unsigned char *)_p;
1670
1671         upper_limit = max_int64 / 256;
1672         lower_limit = min_int64 / 256;
1673
1674         /* Pad with 1 or 0 bits, depending on sign. */
1675         if ((0x40 & *p) == 0x40)
1676                 l = (int64_t)-1;
1677         else
1678                 l = 0;
1679         l = (l << 6) | (0x3f & *p++);
1680         while (--char_cnt > 0) {
1681                 if (l > upper_limit) {
1682                         l = max_int64; /* Truncate on overflow */
1683                         break;
1684                 } else if (l < lower_limit) {
1685                         l = min_int64;
1686                         break;
1687                 }
1688                 l = (l << 8) | (0xff & (int64_t)*p++);
1689         }
1690         return (l);
1691 }
1692
1693 static int
1694 utf8_decode(wchar_t *dest, const char *src, size_t length)
1695 {
1696         size_t n;
1697         int err;
1698
1699         err = 0;
1700         while (length > 0) {
1701                 n = UTF8_mbrtowc(dest, src, length);
1702                 if (n == 0)
1703                         break;
1704                 dest++;
1705                 src += n;
1706                 length -= n;
1707         }
1708         *dest++ = L'\0';
1709         return (err);
1710 }
1711
1712 /*
1713  * Copied and simplified from FreeBSD libc/locale.
1714  */
1715 static size_t
1716 UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n)
1717 {
1718         int ch, i, len, mask;
1719         unsigned long wch;
1720
1721         if (s == NULL || n == 0 || pwc == NULL)
1722                 return (0);
1723
1724         /*
1725          * Determine the number of octets that make up this character from
1726          * the first octet, and a mask that extracts the interesting bits of
1727          * the first octet.
1728          */
1729         ch = (unsigned char)*s;
1730         if ((ch & 0x80) == 0) {
1731                 mask = 0x7f;
1732                 len = 1;
1733         } else if ((ch & 0xe0) == 0xc0) {
1734                 mask = 0x1f;
1735                 len = 2;
1736         } else if ((ch & 0xf0) == 0xe0) {
1737                 mask = 0x0f;
1738                 len = 3;
1739         } else if ((ch & 0xf8) == 0xf0) {
1740                 mask = 0x07;
1741                 len = 4;
1742         } else if ((ch & 0xfc) == 0xf8) {
1743                 mask = 0x03;
1744                 len = 5;
1745         } else if ((ch & 0xfe) == 0xfc) {
1746                 mask = 0x01;
1747                 len = 6;
1748         } else {
1749                 /* Invalid first byte; convert to '?' */
1750                 *pwc = '?';
1751                 return (1);
1752         }
1753
1754         if (n < (size_t)len) {
1755                 /* Invalid first byte; convert to '?' */
1756                 *pwc = '?';
1757                 return (1);
1758         }
1759
1760         /*
1761          * Decode the octet sequence representing the character in chunks
1762          * of 6 bits, most significant first.
1763          */
1764         wch = (unsigned char)*s++ & mask;
1765         i = len;
1766         while (--i != 0) {
1767                 if ((*s & 0xc0) != 0x80) {
1768                         /* Invalid intermediate byte; consume one byte and
1769                          * emit '?' */
1770                         *pwc = '?';
1771                         return (1);
1772                 }
1773                 wch <<= 6;
1774                 wch |= *s++ & 0x3f;
1775         }
1776
1777         /* Assign the value to the output; out-of-range values
1778          * just get truncated. */
1779         *pwc = (wchar_t)wch;
1780 #ifdef WCHAR_MAX
1781         /*
1782          * If platform has WCHAR_MAX, we can do something
1783          * more sensible with out-of-range values.
1784          */
1785         if (wch >= WCHAR_MAX)
1786                 *pwc = '?';
1787 #endif
1788         /* Return number of bytes input consumed: 0 for end-of-string. */
1789         return (wch == L'\0' ? 0 : len);
1790 }
1791
1792
1793 /*
1794  * base64_decode - Base64 decode
1795  *
1796  * This accepts most variations of base-64 encoding, including:
1797  *    * with or without line breaks
1798  *    * with or without the final group padded with '=' or '_' characters
1799  * (The most economical Base-64 variant does not pad the last group and
1800  * omits line breaks; RFC1341 used for MIME requires both.)
1801  */
1802 static char *
1803 base64_decode(const wchar_t *src, size_t len, size_t *out_len)
1804 {
1805         static const unsigned char digits[64] = {
1806                 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
1807                 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
1808                 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
1809                 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
1810                 '4','5','6','7','8','9','+','/' };
1811         static unsigned char decode_table[128];
1812         char *out, *d;
1813
1814         /* If the decode table is not yet initialized, prepare it. */
1815         if (decode_table[digits[1]] != 1) {
1816                 size_t i;
1817                 memset(decode_table, 0xff, sizeof(decode_table));
1818                 for (i = 0; i < sizeof(digits); i++)
1819                         decode_table[digits[i]] = i;
1820         }
1821
1822         /* Allocate enough space to hold the entire output. */
1823         /* Note that we may not use all of this... */
1824         out = (char *)malloc((len * 3 + 3) / 4);
1825         if (out == NULL) {
1826                 *out_len = 0;
1827                 return (NULL);
1828         }
1829         d = out;
1830
1831         while (len > 0) {
1832                 /* Collect the next group of (up to) four characters. */
1833                 int v = 0;
1834                 int group_size = 0;
1835                 while (group_size < 4 && len > 0) {
1836                         /* '=' or '_' padding indicates final group. */
1837                         if (*src == '=' || *src == '_') {
1838                                 len = 0;
1839                                 break;
1840                         }
1841                         /* Skip illegal characters (including line breaks) */
1842                         if (*src > 127 || *src < 32
1843                             || decode_table[*src] == 0xff) {
1844                                 len--;
1845                                 src++;
1846                                 continue;
1847                         }
1848                         v <<= 6;
1849                         v |= decode_table[*src++];
1850                         len --;
1851                         group_size++;
1852                 }
1853                 /* Align a short group properly. */
1854                 v <<= 6 * (4 - group_size);
1855                 /* Unpack the group we just collected. */
1856                 switch (group_size) {
1857                 case 4: d[2] = v & 0xff;
1858                         /* FALLTHROUGH */
1859                 case 3: d[1] = (v >> 8) & 0xff;
1860                         /* FALLTHROUGH */
1861                 case 2: d[0] = (v >> 16) & 0xff;
1862                         break;
1863                 case 1: /* this is invalid! */
1864                         break;
1865                 }
1866                 d += group_size * 3 / 4;
1867         }
1868
1869         *out_len = d - out;
1870         return (out);
1871 }
1872
1873 /*
1874  * This is a little tricky because the C99 standard wcstombs()
1875  * function returns the number of bytes that were converted,
1876  * not the number that should be converted.  As a result,
1877  * we can never accurately size the output buffer (without
1878  * doing a tedious output size calculation in advance).
1879  * This approach (try a conversion, then try again if it fails)
1880  * will almost always succeed on the first try, and is thus
1881  * much faster, at the cost of sometimes requiring multiple
1882  * passes while we expand the buffer.
1883  */
1884 static char *
1885 wide_to_narrow(const wchar_t *wval)
1886 {
1887         int converted_length;
1888         /* Guess an output buffer size and try the conversion. */
1889         int alloc_length = wcslen(wval) * 3;
1890         char *mbs_val = (char *)malloc(alloc_length + 1);
1891         if (mbs_val == NULL)
1892                 return (NULL);
1893         converted_length = wcstombs(mbs_val, wval, alloc_length);
1894
1895         /* If we exhausted the buffer, resize and try again. */
1896         while (converted_length >= alloc_length) {
1897                 free(mbs_val);
1898                 alloc_length *= 2;
1899                 mbs_val = (char *)malloc(alloc_length + 1);
1900                 if (mbs_val == NULL)
1901                         return (NULL);
1902                 converted_length = wcstombs(mbs_val, wval, alloc_length);
1903         }
1904
1905         /* Ensure a trailing null and return the final string. */
1906         mbs_val[alloc_length] = '\0';
1907         return (mbs_val);
1908 }
1909
1910 static char *
1911 url_decode(const char *in)
1912 {
1913         char *out, *d;
1914         const char *s;
1915
1916         out = (char *)malloc(strlen(in) + 1);
1917         if (out == NULL)
1918                 return (NULL);
1919         for (s = in, d = out; *s != '\0'; ) {
1920                 if (*s == '%') {
1921                         /* Try to convert % escape */
1922                         int digit1 = tohex(s[1]);
1923                         int digit2 = tohex(s[2]);
1924                         if (digit1 >= 0 && digit2 >= 0) {
1925                                 /* Looks good, consume three chars */
1926                                 s += 3;
1927                                 /* Convert output */
1928                                 *d++ = ((digit1 << 4) | digit2);
1929                                 continue;
1930                         }
1931                         /* Else fall through and treat '%' as normal char */
1932                 }
1933                 *d++ = *s++;
1934         }
1935         *d = '\0';
1936         return (out);
1937 }
1938
1939 static int
1940 tohex(int c)
1941 {
1942         if (c >= '0' && c <= '9')
1943                 return (c - '0');
1944         else if (c >= 'A' && c <= 'F')
1945                 return (c - 'A' + 10);
1946         else if (c >= 'a' && c <= 'f')
1947                 return (c - 'a' + 10);
1948         else
1949                 return (-1);
1950 }