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