2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
42 #include "archive_acl_private.h" /* For ACL parsing routines. */
43 #include "archive_entry.h"
44 #include "archive_entry_locale.h"
45 #include "archive_private.h"
46 #include "archive_read_private.h"
48 #define tar_min(a,b) ((a) < (b) ? (a) : (b))
51 * Layout of POSIX 'ustar' tar header.
53 struct archive_entry_header_ustar {
62 char linkname[100]; /* "old format" header ends here */
63 char magic[6]; /* For POSIX: "ustar\0" */
64 char version[2]; /* For POSIX: "00" */
73 * Structure of GNU tar header
80 struct archive_entry_header_gnutar {
90 char magic[8]; /* "ustar \0" (note blank/blank/null at end) */
100 struct gnu_sparse sparse[4];
104 * Old GNU format doesn't use POSIX 'prefix' field; they use
105 * the 'L' (longname) entry instead.
110 * Data specific to this format.
112 struct sparse_block {
113 struct sparse_block *next;
120 struct archive_string acl_text;
121 struct archive_string entry_pathname;
122 /* For "GNU.sparse.name" and other similar path extensions. */
123 struct archive_string entry_pathname_override;
124 struct archive_string entry_linkpath;
125 struct archive_string entry_uname;
126 struct archive_string entry_gname;
127 struct archive_string longlink;
128 struct archive_string longname;
129 struct archive_string pax_header;
130 struct archive_string pax_global;
131 struct archive_string line;
132 int pax_hdrcharset_binary;
133 int header_recursion_depth;
134 int64_t entry_bytes_remaining;
135 int64_t entry_offset;
136 int64_t entry_padding;
137 int64_t entry_bytes_unconsumed;
139 struct sparse_block *sparse_list;
140 struct sparse_block *sparse_last;
141 int64_t sparse_offset;
142 int64_t sparse_numbytes;
143 int sparse_gnu_major;
144 int sparse_gnu_minor;
145 char sparse_gnu_pending;
147 struct archive_string localname;
148 struct archive_string_conv *opt_sconv;
149 struct archive_string_conv *sconv;
150 struct archive_string_conv *sconv_acl;
151 struct archive_string_conv *sconv_default;
152 int init_default_conversion;
154 int process_mac_extensions;
155 int read_concatenated_archives;
158 static int archive_block_is_null(const char *p);
159 static char *base64_decode(const char *, size_t, size_t *);
160 static int gnu_add_sparse_entry(struct archive_read *, struct tar *,
161 int64_t offset, int64_t remaining);
163 static void gnu_clear_sparse_list(struct tar *);
164 static int gnu_sparse_old_read(struct archive_read *, struct tar *,
165 const struct archive_entry_header_gnutar *header, size_t *);
166 static int gnu_sparse_old_parse(struct archive_read *, struct tar *,
167 const struct gnu_sparse *sparse, int length);
168 static int gnu_sparse_01_parse(struct archive_read *, struct tar *,
170 static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *,
172 static int header_Solaris_ACL(struct archive_read *, struct tar *,
173 struct archive_entry *, const void *, size_t *);
174 static int header_common(struct archive_read *, struct tar *,
175 struct archive_entry *, const void *);
176 static int header_old_tar(struct archive_read *, struct tar *,
177 struct archive_entry *, const void *);
178 static int header_pax_extensions(struct archive_read *, struct tar *,
179 struct archive_entry *, const void *, size_t *);
180 static int header_pax_global(struct archive_read *, struct tar *,
181 struct archive_entry *, const void *h, size_t *);
182 static int header_longlink(struct archive_read *, struct tar *,
183 struct archive_entry *, const void *h, size_t *);
184 static int header_longname(struct archive_read *, struct tar *,
185 struct archive_entry *, const void *h, size_t *);
186 static int read_mac_metadata_blob(struct archive_read *, struct tar *,
187 struct archive_entry *, const void *h, size_t *);
188 static int header_volume(struct archive_read *, struct tar *,
189 struct archive_entry *, const void *h, size_t *);
190 static int header_ustar(struct archive_read *, struct tar *,
191 struct archive_entry *, const void *h);
192 static int header_gnutar(struct archive_read *, struct tar *,
193 struct archive_entry *, const void *h, size_t *);
194 static int archive_read_format_tar_bid(struct archive_read *, int);
195 static int archive_read_format_tar_options(struct archive_read *,
196 const char *, const char *);
197 static int archive_read_format_tar_cleanup(struct archive_read *);
198 static int archive_read_format_tar_read_data(struct archive_read *a,
199 const void **buff, size_t *size, int64_t *offset);
200 static int archive_read_format_tar_skip(struct archive_read *a);
201 static int archive_read_format_tar_read_header(struct archive_read *,
202 struct archive_entry *);
203 static int checksum(struct archive_read *, const void *);
204 static int pax_attribute(struct archive_read *, struct tar *,
205 struct archive_entry *, const char *key, const char *value);
206 static int pax_header(struct archive_read *, struct tar *,
207 struct archive_entry *, char *attr);
208 static void pax_time(const char *, int64_t *sec, long *nanos);
209 static ssize_t readline(struct archive_read *, struct tar *, const char **,
210 ssize_t limit, size_t *);
211 static int read_body_to_string(struct archive_read *, struct tar *,
212 struct archive_string *, const void *h, size_t *);
213 static int solaris_sparse_parse(struct archive_read *, struct tar *,
214 struct archive_entry *, const char *);
215 static int64_t tar_atol(const char *, size_t);
216 static int64_t tar_atol10(const char *, size_t);
217 static int64_t tar_atol256(const char *, size_t);
218 static int64_t tar_atol8(const char *, size_t);
219 static int tar_read_header(struct archive_read *, struct tar *,
220 struct archive_entry *, size_t *);
221 static int tohex(int c);
222 static char *url_decode(const char *);
223 static void tar_flush_unconsumed(struct archive_read *, size_t *);
227 archive_read_support_format_gnutar(struct archive *a)
229 archive_check_magic(a, ARCHIVE_READ_MAGIC,
230 ARCHIVE_STATE_NEW, "archive_read_support_format_gnutar");
231 return (archive_read_support_format_tar(a));
236 archive_read_support_format_tar(struct archive *_a)
238 struct archive_read *a = (struct archive_read *)_a;
242 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
243 ARCHIVE_STATE_NEW, "archive_read_support_format_tar");
245 tar = (struct tar *)calloc(1, sizeof(*tar));
246 #ifdef HAVE_COPYFILE_H
247 /* Set this by default on Mac OS. */
248 tar->process_mac_extensions = 1;
251 archive_set_error(&a->archive, ENOMEM,
252 "Can't allocate tar data");
253 return (ARCHIVE_FATAL);
256 r = __archive_read_register_format(a, tar, "tar",
257 archive_read_format_tar_bid,
258 archive_read_format_tar_options,
259 archive_read_format_tar_read_header,
260 archive_read_format_tar_read_data,
261 archive_read_format_tar_skip,
263 archive_read_format_tar_cleanup,
273 archive_read_format_tar_cleanup(struct archive_read *a)
277 tar = (struct tar *)(a->format->data);
278 gnu_clear_sparse_list(tar);
279 archive_string_free(&tar->acl_text);
280 archive_string_free(&tar->entry_pathname);
281 archive_string_free(&tar->entry_pathname_override);
282 archive_string_free(&tar->entry_linkpath);
283 archive_string_free(&tar->entry_uname);
284 archive_string_free(&tar->entry_gname);
285 archive_string_free(&tar->line);
286 archive_string_free(&tar->pax_global);
287 archive_string_free(&tar->pax_header);
288 archive_string_free(&tar->longname);
289 archive_string_free(&tar->longlink);
290 archive_string_free(&tar->localname);
292 (a->format->data) = NULL;
298 archive_read_format_tar_bid(struct archive_read *a, int best_bid)
302 const struct archive_entry_header_ustar *header;
304 (void)best_bid; /* UNUSED */
308 /* Now let's look at the actual header and see if it matches. */
309 h = __archive_read_ahead(a, 512, NULL);
313 /* If it's an end-of-archive mark, we can handle it. */
314 if (h[0] == 0 && archive_block_is_null(h)) {
316 * Usually, I bid the number of bits verified, but
317 * in this case, 4096 seems excessive so I picked 10 as
318 * an arbitrary but reasonable-seeming value.
323 /* If it's not an end-of-archive mark, it must have a valid checksum.*/
326 bid += 48; /* Checksum is usually 6 octal digits. */
328 header = (const struct archive_entry_header_ustar *)h;
330 /* Recognize POSIX formats. */
331 if ((memcmp(header->magic, "ustar\0", 6) == 0)
332 && (memcmp(header->version, "00", 2) == 0))
335 /* Recognize GNU tar format. */
336 if ((memcmp(header->magic, "ustar ", 6) == 0)
337 && (memcmp(header->version, " \0", 2) == 0))
340 /* Type flag must be null, digit or A-Z, a-z. */
341 if (header->typeflag[0] != 0 &&
342 !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
343 !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
344 !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
346 bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
348 /* Sanity check: Look at first byte of mode field. */
349 switch (255 & (unsigned)header->mode[0]) {
351 /* Base-256 value: No further verification possible! */
353 case ' ': /* Not recommended, but not illegal, either. */
355 case '0': case '1': case '2': case '3':
356 case '4': case '5': case '6': case '7':
358 /* TODO: Check format of remainder of this field. */
361 /* Not a valid mode; bail out here. */
364 /* TODO: Sanity test uid/gid/size/mtime/rdevmajor/rdevminor fields. */
370 archive_read_format_tar_options(struct archive_read *a,
371 const char *key, const char *val)
374 int ret = ARCHIVE_FAILED;
376 tar = (struct tar *)(a->format->data);
377 if (strcmp(key, "compat-2x") == 0) {
378 /* Handle UTF-8 filnames as libarchive 2.x */
379 tar->compat_2x = (val != NULL && val[0] != 0);
380 tar->init_default_conversion = tar->compat_2x;
382 } else if (strcmp(key, "hdrcharset") == 0) {
383 if (val == NULL || val[0] == 0)
384 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
385 "tar: hdrcharset option needs a character-set name");
388 archive_string_conversion_from_charset(
389 &a->archive, val, 0);
390 if (tar->opt_sconv != NULL)
396 } else if (strcmp(key, "mac-ext") == 0) {
397 tar->process_mac_extensions = (val != NULL && val[0] != 0);
399 } else if (strcmp(key, "read_concatenated_archives") == 0) {
400 tar->read_concatenated_archives = (val != NULL && val[0] != 0);
404 /* Note: The "warn" return is just to inform the options
405 * supervisor that we didn't handle it. It will generate
406 * a suitable error if no one used this option. */
407 return (ARCHIVE_WARN);
410 /* utility function- this exists to centralize the logic of tracking
411 * how much unconsumed data we have floating around, and to consume
412 * anything outstanding since we're going to do read_aheads
415 tar_flush_unconsumed(struct archive_read *a, size_t *unconsumed)
419 void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL);
420 * this block of code is to poison claimed unconsumed space, ensuring
421 * things break if it is in use still.
422 * currently it WILL break things, so enable it only for debugging this issue
424 memset(data, 0xff, *unconsumed);
427 __archive_read_consume(a, *unconsumed);
433 * The function invoked by archive_read_next_header(). This
434 * just sets up a few things and then calls the internal
435 * tar_read_header() function below.
438 archive_read_format_tar_read_header(struct archive_read *a,
439 struct archive_entry *entry)
442 * When converting tar archives to cpio archives, it is
443 * essential that each distinct file have a distinct inode
444 * number. To simplify this, we keep a static count here to
445 * assign fake dev/inode numbers to each tar entry. Note that
446 * pax format archives may overwrite this with something more
449 * Ideally, we would track every file read from the archive so
450 * that we could assign the same dev/ino pair to hardlinks,
451 * but the memory required to store a complete lookup table is
452 * probably not worthwhile just to support the relatively
453 * obscure tar->cpio conversion case.
455 static int default_inode;
456 static int default_dev;
461 size_t l, unconsumed = 0;
463 /* Assign default device/inode values. */
464 archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
465 archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */
466 /* Limit generated st_ino number to 16 bits. */
467 if (default_inode >= 0xffff) {
472 tar = (struct tar *)(a->format->data);
473 tar->entry_offset = 0;
474 gnu_clear_sparse_list(tar);
475 tar->realsize = -1; /* Mark this as "unset" */
477 /* Setup default string conversion. */
478 tar->sconv = tar->opt_sconv;
479 if (tar->sconv == NULL) {
480 if (!tar->init_default_conversion) {
482 archive_string_default_conversion_for_read(&(a->archive));
483 tar->init_default_conversion = 1;
485 tar->sconv = tar->sconv_default;
488 r = tar_read_header(a, tar, entry, &unconsumed);
490 tar_flush_unconsumed(a, &unconsumed);
493 * "non-sparse" files are really just sparse files with
496 if (tar->sparse_list == NULL) {
497 if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining)
499 return (ARCHIVE_FATAL);
501 struct sparse_block *sb;
503 for (sb = tar->sparse_list; sb != NULL; sb = sb->next) {
505 archive_entry_sparse_add_entry(entry,
506 sb->offset, sb->remaining);
510 if (r == ARCHIVE_OK && archive_entry_filetype(entry) == AE_IFREG) {
512 * "Regular" entry with trailing '/' is really
513 * directory: This is needed for certain old tar
514 * variants and even for some broken newer ones.
516 if ((wp = archive_entry_pathname_w(entry)) != NULL) {
518 if (l > 0 && wp[l - 1] == L'/') {
519 archive_entry_set_filetype(entry, AE_IFDIR);
521 } else if ((p = archive_entry_pathname(entry)) != NULL) {
523 if (l > 0 && p[l - 1] == '/') {
524 archive_entry_set_filetype(entry, AE_IFDIR);
532 archive_read_format_tar_read_data(struct archive_read *a,
533 const void **buff, size_t *size, int64_t *offset)
537 struct sparse_block *p;
539 tar = (struct tar *)(a->format->data);
542 /* Remove exhausted entries from sparse list. */
543 while (tar->sparse_list != NULL &&
544 tar->sparse_list->remaining == 0) {
545 p = tar->sparse_list;
546 tar->sparse_list = p->next;
550 if (tar->entry_bytes_unconsumed) {
551 __archive_read_consume(a, tar->entry_bytes_unconsumed);
552 tar->entry_bytes_unconsumed = 0;
555 /* If we're at end of file, return EOF. */
556 if (tar->sparse_list == NULL ||
557 tar->entry_bytes_remaining == 0) {
558 if (__archive_read_consume(a, tar->entry_padding) < 0)
559 return (ARCHIVE_FATAL);
560 tar->entry_padding = 0;
563 *offset = tar->realsize;
564 return (ARCHIVE_EOF);
567 *buff = __archive_read_ahead(a, 1, &bytes_read);
569 return (ARCHIVE_FATAL);
571 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
572 "Truncated tar archive");
573 return (ARCHIVE_FATAL);
575 if (bytes_read > tar->entry_bytes_remaining)
576 bytes_read = (ssize_t)tar->entry_bytes_remaining;
577 /* Don't read more than is available in the
578 * current sparse block. */
579 if (tar->sparse_list->remaining < bytes_read)
580 bytes_read = (ssize_t)tar->sparse_list->remaining;
582 *offset = tar->sparse_list->offset;
583 tar->sparse_list->remaining -= bytes_read;
584 tar->sparse_list->offset += bytes_read;
585 tar->entry_bytes_remaining -= bytes_read;
586 tar->entry_bytes_unconsumed = bytes_read;
588 if (!tar->sparse_list->hole)
590 /* Current is hole data and skip this. */
595 archive_read_format_tar_skip(struct archive_read *a)
597 int64_t bytes_skipped;
599 struct sparse_block *p;
602 tar = (struct tar *)(a->format->data);
604 /* Do not consume the hole of a sparse file. */
606 for (p = tar->sparse_list; p != NULL; p = p->next) {
608 if (p->remaining >= INT64_MAX - request) {
609 return ARCHIVE_FATAL;
611 request += p->remaining;
614 if (request > tar->entry_bytes_remaining)
615 request = tar->entry_bytes_remaining;
616 request += tar->entry_padding + tar->entry_bytes_unconsumed;
618 bytes_skipped = __archive_read_consume(a, request);
619 if (bytes_skipped < 0)
620 return (ARCHIVE_FATAL);
622 tar->entry_bytes_remaining = 0;
623 tar->entry_bytes_unconsumed = 0;
624 tar->entry_padding = 0;
626 /* Free the sparse list. */
627 gnu_clear_sparse_list(tar);
633 * This function recursively interprets all of the headers associated
634 * with a single entry.
637 tar_read_header(struct archive_read *a, struct tar *tar,
638 struct archive_entry *entry, size_t *unconsumed)
643 const struct archive_entry_header_ustar *header;
644 const struct archive_entry_header_gnutar *gnuheader;
646 /* Loop until we find a workable header record. */
648 tar_flush_unconsumed(a, unconsumed);
650 /* Read 512-byte header record */
651 h = __archive_read_ahead(a, 512, &bytes);
654 if (bytes == 0) { /* EOF at a block boundary. */
655 /* Some writers do omit the block of nulls. <sigh> */
656 return (ARCHIVE_EOF);
658 if (bytes < 512) { /* Short block at EOF; this is bad. */
659 archive_set_error(&a->archive,
660 ARCHIVE_ERRNO_FILE_FORMAT,
661 "Truncated tar archive");
662 return (ARCHIVE_FATAL);
666 /* Header is workable if it's not an end-of-archive mark. */
667 if (h[0] != 0 || !archive_block_is_null(h))
670 /* Ensure format is set for archives with only null blocks. */
671 if (a->archive.archive_format_name == NULL) {
672 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
673 a->archive.archive_format_name = "tar";
676 if (!tar->read_concatenated_archives) {
677 /* Try to consume a second all-null record, as well. */
678 tar_flush_unconsumed(a, unconsumed);
679 h = __archive_read_ahead(a, 512, NULL);
680 if (h != NULL && h[0] == 0 && archive_block_is_null(h))
681 __archive_read_consume(a, 512);
682 archive_clear_error(&a->archive);
683 return (ARCHIVE_EOF);
687 * We're reading concatenated archives, ignore this block and
688 * loop to get the next.
693 * Note: If the checksum fails and we return ARCHIVE_RETRY,
694 * then the client is likely to just retry. This is a very
695 * crude way to search for the next valid header!
697 * TODO: Improve this by implementing a real header scan.
699 if (!checksum(a, h)) {
700 tar_flush_unconsumed(a, unconsumed);
701 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
702 return (ARCHIVE_RETRY); /* Retryable: Invalid header */
705 if (++tar->header_recursion_depth > 32) {
706 tar_flush_unconsumed(a, unconsumed);
707 archive_set_error(&a->archive, EINVAL, "Too many special headers");
708 return (ARCHIVE_WARN);
711 /* Determine the format variant. */
712 header = (const struct archive_entry_header_ustar *)h;
714 switch(header->typeflag[0]) {
715 case 'A': /* Solaris tar ACL */
716 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
717 a->archive.archive_format_name = "Solaris tar";
718 err = header_Solaris_ACL(a, tar, entry, h, unconsumed);
720 case 'g': /* POSIX-standard 'g' header. */
721 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
722 a->archive.archive_format_name = "POSIX pax interchange format";
723 err = header_pax_global(a, tar, entry, h, unconsumed);
724 if (err == ARCHIVE_EOF)
727 case 'K': /* Long link name (GNU tar, others) */
728 err = header_longlink(a, tar, entry, h, unconsumed);
730 case 'L': /* Long filename (GNU tar, others) */
731 err = header_longname(a, tar, entry, h, unconsumed);
733 case 'V': /* GNU volume header */
734 err = header_volume(a, tar, entry, h, unconsumed);
736 case 'X': /* Used by SUN tar; same as 'x'. */
737 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
738 a->archive.archive_format_name =
739 "POSIX pax interchange format (Sun variant)";
740 err = header_pax_extensions(a, tar, entry, h, unconsumed);
742 case 'x': /* POSIX-standard 'x' header. */
743 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
744 a->archive.archive_format_name = "POSIX pax interchange format";
745 err = header_pax_extensions(a, tar, entry, h, unconsumed);
748 gnuheader = (const struct archive_entry_header_gnutar *)h;
749 if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) {
750 a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
751 a->archive.archive_format_name = "GNU tar format";
752 err = header_gnutar(a, tar, entry, h, unconsumed);
753 } else if (memcmp(header->magic, "ustar", 5) == 0) {
754 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
755 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
756 a->archive.archive_format_name = "POSIX ustar format";
758 err = header_ustar(a, tar, entry, h);
760 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
761 a->archive.archive_format_name = "tar (non-POSIX)";
762 err = header_old_tar(a, tar, entry, h);
765 if (err == ARCHIVE_FATAL)
768 tar_flush_unconsumed(a, unconsumed);
773 --tar->header_recursion_depth;
774 /* Yuck. Apple's design here ends up storing long pathname
775 * extensions for both the AppleDouble extension entry and the
778 if ((err == ARCHIVE_WARN || err == ARCHIVE_OK) &&
779 tar->header_recursion_depth == 0 &&
780 tar->process_mac_extensions) {
781 int err2 = read_mac_metadata_blob(a, tar, entry, h, unconsumed);
786 /* We return warnings or success as-is. Anything else is fatal. */
787 if (err == ARCHIVE_WARN || err == ARCHIVE_OK) {
788 if (tar->sparse_gnu_pending) {
789 if (tar->sparse_gnu_major == 1 &&
790 tar->sparse_gnu_minor == 0) {
793 tar->sparse_gnu_pending = 0;
794 /* Read initial sparse map. */
795 bytes_read = gnu_sparse_10_read(a, tar, unconsumed);
796 tar->entry_bytes_remaining -= bytes_read;
798 return ((int)bytes_read);
800 archive_set_error(&a->archive,
802 "Unrecognized GNU sparse file format");
803 return (ARCHIVE_WARN);
805 tar->sparse_gnu_pending = 0;
809 if (err == ARCHIVE_EOF)
810 /* EOF when recursively reading a header is bad. */
811 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
812 return (ARCHIVE_FATAL);
816 * Return true if block checksum is correct.
819 checksum(struct archive_read *a, const void *h)
821 const unsigned char *bytes;
822 const struct archive_entry_header_ustar *header;
826 (void)a; /* UNUSED */
827 bytes = (const unsigned char *)h;
828 header = (const struct archive_entry_header_ustar *)h;
830 /* Checksum field must hold an octal number */
831 for (i = 0; i < sizeof(header->checksum); ++i) {
832 char c = header->checksum[i];
833 if (c != ' ' && c != '\0' && (c < '0' || c > '7'))
838 * Test the checksum. Note that POSIX specifies _unsigned_
839 * bytes for this calculation.
841 sum = (int)tar_atol(header->checksum, sizeof(header->checksum));
843 for (i = 0; i < 148; i++)
844 check += (unsigned char)bytes[i];
848 check += (unsigned char)bytes[i];
853 * Repeat test with _signed_ bytes, just in case this archive
854 * was created by an old BSD, Solaris, or HP-UX tar with a
855 * broken checksum calculation.
858 for (i = 0; i < 148; i++)
859 check += (signed char)bytes[i];
863 check += (signed char)bytes[i];
871 * Return true if this block contains only nulls.
874 archive_block_is_null(const char *p)
878 for (i = 0; i < 512; i++)
885 * Interpret 'A' Solaris ACL header
888 header_Solaris_ACL(struct archive_read *a, struct tar *tar,
889 struct archive_entry *entry, const void *h, size_t *unconsumed)
891 const struct archive_entry_header_ustar *header;
898 * read_body_to_string adds a NUL terminator, but we need a little
899 * more to make sure that we don't overrun acl_text later.
901 header = (const struct archive_entry_header_ustar *)h;
902 size = (size_t)tar_atol(header->size, sizeof(header->size));
903 err = read_body_to_string(a, tar, &(tar->acl_text), h, unconsumed);
904 if (err != ARCHIVE_OK)
907 /* Recursively read next header */
908 err = tar_read_header(a, tar, entry, unconsumed);
909 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
912 /* TODO: Examine the first characters to see if this
913 * is an AIX ACL descriptor. We'll likely never support
914 * them, but it would be polite to recognize and warn when
917 /* Leading octal number indicates ACL type and number of entries. */
918 p = acl = tar->acl_text.s;
920 while (*p != '\0' && p < acl + size) {
921 if (*p < '0' || *p > '7') {
922 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
923 "Malformed Solaris ACL attribute (invalid digit)");
924 return(ARCHIVE_WARN);
928 if (type > 077777777) {
929 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
930 "Malformed Solaris ACL attribute (count too large)");
931 return (ARCHIVE_WARN);
935 switch ((int)type & ~0777777) {
940 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
941 "Solaris NFSv4 ACLs not supported");
942 return (ARCHIVE_WARN);
944 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
945 "Malformed Solaris ACL attribute (unsupported type %o)",
947 return (ARCHIVE_WARN);
951 if (p >= acl + size) {
952 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
953 "Malformed Solaris ACL attribute (body overflow)");
954 return(ARCHIVE_WARN);
957 /* ACL text is null-terminated; find the end. */
961 while (*p != '\0' && p < acl + size)
964 if (tar->sconv_acl == NULL) {
965 tar->sconv_acl = archive_string_conversion_from_charset(
966 &(a->archive), "UTF-8", 1);
967 if (tar->sconv_acl == NULL)
968 return (ARCHIVE_FATAL);
970 archive_strncpy(&(tar->localname), acl, p - acl);
971 err = archive_acl_parse_l(archive_entry_acl(entry),
972 tar->localname.s, ARCHIVE_ENTRY_ACL_TYPE_ACCESS, tar->sconv_acl);
973 if (err != ARCHIVE_OK) {
974 if (errno == ENOMEM) {
975 archive_set_error(&a->archive, ENOMEM,
976 "Can't allocate memory for ACL");
978 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
979 "Malformed Solaris ACL attribute (unparsable)");
985 * Interpret 'K' long linkname header.
988 header_longlink(struct archive_read *a, struct tar *tar,
989 struct archive_entry *entry, const void *h, size_t *unconsumed)
993 err = read_body_to_string(a, tar, &(tar->longlink), h, unconsumed);
994 if (err != ARCHIVE_OK)
996 err = tar_read_header(a, tar, entry, unconsumed);
997 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
999 /* Set symlink if symlink already set, else hardlink. */
1000 archive_entry_copy_link(entry, tar->longlink.s);
1001 return (ARCHIVE_OK);
1005 set_conversion_failed_error(struct archive_read *a,
1006 struct archive_string_conv *sconv, const char *name)
1008 if (errno == ENOMEM) {
1009 archive_set_error(&a->archive, ENOMEM,
1010 "Can't allocate memory for %s", name);
1011 return (ARCHIVE_FATAL);
1013 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1014 "%s can't be converted from %s to current locale.",
1015 name, archive_string_conversion_charset_name(sconv));
1016 return (ARCHIVE_WARN);
1020 * Interpret 'L' long filename header.
1023 header_longname(struct archive_read *a, struct tar *tar,
1024 struct archive_entry *entry, const void *h, size_t *unconsumed)
1028 err = read_body_to_string(a, tar, &(tar->longname), h, unconsumed);
1029 if (err != ARCHIVE_OK)
1031 /* Read and parse "real" header, then override name. */
1032 err = tar_read_header(a, tar, entry, unconsumed);
1033 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1035 if (archive_entry_copy_pathname_l(entry, tar->longname.s,
1036 archive_strlen(&(tar->longname)), tar->sconv) != 0)
1037 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1043 * Interpret 'V' GNU tar volume header.
1046 header_volume(struct archive_read *a, struct tar *tar,
1047 struct archive_entry *entry, const void *h, size_t *unconsumed)
1051 /* Just skip this and read the next header. */
1052 return (tar_read_header(a, tar, entry, unconsumed));
1056 * Read body of an archive entry into an archive_string object.
1059 read_body_to_string(struct archive_read *a, struct tar *tar,
1060 struct archive_string *as, const void *h, size_t *unconsumed)
1063 const struct archive_entry_header_ustar *header;
1066 (void)tar; /* UNUSED */
1067 header = (const struct archive_entry_header_ustar *)h;
1068 size = tar_atol(header->size, sizeof(header->size));
1069 if ((size > 1048576) || (size < 0)) {
1070 archive_set_error(&a->archive, EINVAL,
1071 "Special header too large");
1072 return (ARCHIVE_FATAL);
1075 /* Fail if we can't make our buffer big enough. */
1076 if (archive_string_ensure(as, (size_t)size+1) == NULL) {
1077 archive_set_error(&a->archive, ENOMEM,
1079 return (ARCHIVE_FATAL);
1082 tar_flush_unconsumed(a, unconsumed);
1084 /* Read the body into the string. */
1085 *unconsumed = (size_t)((size + 511) & ~ 511);
1086 src = __archive_read_ahead(a, *unconsumed, NULL);
1089 return (ARCHIVE_FATAL);
1091 memcpy(as->s, src, (size_t)size);
1093 as->length = (size_t)size;
1094 return (ARCHIVE_OK);
1098 * Parse out common header elements.
1100 * This would be the same as header_old_tar, except that the
1101 * filename is handled slightly differently for old and POSIX
1102 * entries (POSIX entries support a 'prefix'). This factoring
1103 * allows header_old_tar and header_ustar
1104 * to handle filenames differently, while still putting most of the
1105 * common parsing into one place.
1108 header_common(struct archive_read *a, struct tar *tar,
1109 struct archive_entry *entry, const void *h)
1111 const struct archive_entry_header_ustar *header;
1113 int err = ARCHIVE_OK;
1115 header = (const struct archive_entry_header_ustar *)h;
1116 if (header->linkname[0])
1117 archive_strncpy(&(tar->entry_linkpath),
1118 header->linkname, sizeof(header->linkname));
1120 archive_string_empty(&(tar->entry_linkpath));
1122 /* Parse out the numeric fields (all are octal) */
1123 archive_entry_set_mode(entry,
1124 (mode_t)tar_atol(header->mode, sizeof(header->mode)));
1125 archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
1126 archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
1127 tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size));
1128 if (tar->entry_bytes_remaining < 0) {
1129 tar->entry_bytes_remaining = 0;
1130 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1131 "Tar entry has negative size");
1132 return (ARCHIVE_FATAL);
1134 if (tar->entry_bytes_remaining == INT64_MAX) {
1135 /* Note: tar_atol returns INT64_MAX on overflow */
1136 tar->entry_bytes_remaining = 0;
1137 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1138 "Tar entry size overflow");
1139 return (ARCHIVE_FATAL);
1141 tar->realsize = tar->entry_bytes_remaining;
1142 archive_entry_set_size(entry, tar->entry_bytes_remaining);
1143 archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
1145 /* Handle the tar type flag appropriately. */
1146 tartype = header->typeflag[0];
1149 case '1': /* Hard link */
1150 if (archive_entry_copy_hardlink_l(entry, tar->entry_linkpath.s,
1151 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1152 err = set_conversion_failed_error(a, tar->sconv,
1154 if (err == ARCHIVE_FATAL)
1158 * The following may seem odd, but: Technically, tar
1159 * does not store the file type for a "hard link"
1160 * entry, only the fact that it is a hard link. So, I
1161 * leave the type zero normally. But, pax interchange
1162 * format allows hard links to have data, which
1163 * implies that the underlying entry is a regular
1166 if (archive_entry_size(entry) > 0)
1167 archive_entry_set_filetype(entry, AE_IFREG);
1170 * A tricky point: Traditionally, tar readers have
1171 * ignored the size field when reading hardlink
1172 * entries, and some writers put non-zero sizes even
1173 * though the body is empty. POSIX blessed this
1174 * convention in the 1988 standard, but broke with
1175 * this tradition in 2001 by permitting hardlink
1176 * entries to store valid bodies in pax interchange
1177 * format, but not in ustar format. Since there is no
1178 * hard and fast way to distinguish pax interchange
1179 * from earlier archives (the 'x' and 'g' entries are
1180 * optional, after all), we need a heuristic.
1182 if (archive_entry_size(entry) == 0) {
1183 /* If the size is already zero, we're done. */
1184 } else if (a->archive.archive_format
1185 == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
1186 /* Definitely pax extended; must obey hardlink size. */
1187 } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR
1188 || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR)
1190 /* Old-style or GNU tar: we must ignore the size. */
1191 archive_entry_set_size(entry, 0);
1192 tar->entry_bytes_remaining = 0;
1193 } else if (archive_read_format_tar_bid(a, 50) > 50) {
1195 * We don't know if it's pax: If the bid
1196 * function sees a valid ustar header
1197 * immediately following, then let's ignore
1198 * the hardlink size.
1200 archive_entry_set_size(entry, 0);
1201 tar->entry_bytes_remaining = 0;
1204 * TODO: There are still two cases I'd like to handle:
1205 * = a ustar non-pax archive with a hardlink entry at
1206 * end-of-archive. (Look for block of nulls following?)
1207 * = a pax archive that has not seen any pax headers
1208 * and has an entry which is a hardlink entry storing
1209 * a body containing an uncompressed tar archive.
1210 * The first is worth addressing; I don't see any reliable
1211 * way to deal with the second possibility.
1214 case '2': /* Symlink */
1215 archive_entry_set_filetype(entry, AE_IFLNK);
1216 archive_entry_set_size(entry, 0);
1217 tar->entry_bytes_remaining = 0;
1218 if (archive_entry_copy_symlink_l(entry, tar->entry_linkpath.s,
1219 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1220 err = set_conversion_failed_error(a, tar->sconv,
1222 if (err == ARCHIVE_FATAL)
1226 case '3': /* Character device */
1227 archive_entry_set_filetype(entry, AE_IFCHR);
1228 archive_entry_set_size(entry, 0);
1229 tar->entry_bytes_remaining = 0;
1231 case '4': /* Block device */
1232 archive_entry_set_filetype(entry, AE_IFBLK);
1233 archive_entry_set_size(entry, 0);
1234 tar->entry_bytes_remaining = 0;
1237 archive_entry_set_filetype(entry, AE_IFDIR);
1238 archive_entry_set_size(entry, 0);
1239 tar->entry_bytes_remaining = 0;
1241 case '6': /* FIFO device */
1242 archive_entry_set_filetype(entry, AE_IFIFO);
1243 archive_entry_set_size(entry, 0);
1244 tar->entry_bytes_remaining = 0;
1246 case 'D': /* GNU incremental directory type */
1248 * No special handling is actually required here.
1249 * It might be nice someday to preprocess the file list and
1250 * provide it to the client, though.
1252 archive_entry_set_filetype(entry, AE_IFDIR);
1254 case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
1256 * As far as I can tell, this is just like a regular file
1257 * entry, except that the contents should be _appended_ to
1258 * the indicated file at the indicated offset. This may
1259 * require some API work to fully support.
1262 case 'N': /* Old GNU "long filename" entry. */
1263 /* The body of this entry is a script for renaming
1264 * previously-extracted entries. Ugh. It will never
1265 * be supported by libarchive. */
1266 archive_entry_set_filetype(entry, AE_IFREG);
1268 case 'S': /* GNU sparse files */
1270 * Sparse files are really just regular files with
1271 * sparse information in the extended area.
1274 default: /* Regular file and non-standard types */
1276 * Per POSIX: non-recognized types should always be
1277 * treated as regular files.
1279 archive_entry_set_filetype(entry, AE_IFREG);
1286 * Parse out header elements for "old-style" tar archives.
1289 header_old_tar(struct archive_read *a, struct tar *tar,
1290 struct archive_entry *entry, const void *h)
1292 const struct archive_entry_header_ustar *header;
1293 int err = ARCHIVE_OK, err2;
1295 /* Copy filename over (to ensure null termination). */
1296 header = (const struct archive_entry_header_ustar *)h;
1297 if (archive_entry_copy_pathname_l(entry,
1298 header->name, sizeof(header->name), tar->sconv) != 0) {
1299 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1300 if (err == ARCHIVE_FATAL)
1304 /* Grab rest of common fields */
1305 err2 = header_common(a, tar, entry, h);
1309 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1314 * Read a Mac AppleDouble-encoded blob of file metadata,
1318 read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
1319 struct archive_entry *entry, const void *h, size_t *unconsumed)
1323 const char *p, *name;
1324 const wchar_t *wp, *wname;
1326 (void)h; /* UNUSED */
1328 wname = wp = archive_entry_pathname_w(entry);
1330 /* Find the last path element. */
1331 for (; *wp != L'\0'; ++wp) {
1332 if (wp[0] == '/' && wp[1] != L'\0')
1336 * If last path element starts with "._", then
1337 * this is a Mac extension.
1339 if (wname[0] != L'.' || wname[1] != L'_' || wname[2] == L'\0')
1342 /* Find the last path element. */
1343 name = p = archive_entry_pathname(entry);
1345 return (ARCHIVE_FAILED);
1346 for (; *p != '\0'; ++p) {
1347 if (p[0] == '/' && p[1] != '\0')
1351 * If last path element starts with "._", then
1352 * this is a Mac extension.
1354 if (name[0] != '.' || name[1] != '_' || name[2] == '\0')
1358 /* Read the body as a Mac OS metadata blob. */
1359 size = archive_entry_size(entry);
1362 * TODO: Look beyond the body here to peek at the next header.
1363 * If it's a regular header (not an extension header)
1364 * that has the wrong name, just return the current
1365 * entry as-is, without consuming the body here.
1366 * That would reduce the risk of us mis-identifying
1367 * an ordinary file that just happened to have
1368 * a name starting with "._".
1370 * Q: Is the above idea really possible? Even
1371 * when there are GNU or pax extension entries?
1373 data = __archive_read_ahead(a, (size_t)size, NULL);
1376 return (ARCHIVE_FATAL);
1378 archive_entry_copy_mac_metadata(entry, data, (size_t)size);
1379 *unconsumed = (size_t)((size + 511) & ~ 511);
1380 tar_flush_unconsumed(a, unconsumed);
1381 return (tar_read_header(a, tar, entry, unconsumed));
1385 * Parse a file header for a pax extended archive entry.
1388 header_pax_global(struct archive_read *a, struct tar *tar,
1389 struct archive_entry *entry, const void *h, size_t *unconsumed)
1393 err = read_body_to_string(a, tar, &(tar->pax_global), h, unconsumed);
1394 if (err != ARCHIVE_OK)
1396 err = tar_read_header(a, tar, entry, unconsumed);
1401 header_pax_extensions(struct archive_read *a, struct tar *tar,
1402 struct archive_entry *entry, const void *h, size_t *unconsumed)
1406 err = read_body_to_string(a, tar, &(tar->pax_header), h, unconsumed);
1407 if (err != ARCHIVE_OK)
1410 /* Parse the next header. */
1411 err = tar_read_header(a, tar, entry, unconsumed);
1412 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1416 * TODO: Parse global/default options into 'entry' struct here
1417 * before handling file-specific options.
1419 * This design (parse standard header, then overwrite with pax
1420 * extended attribute data) usually works well, but isn't ideal;
1421 * it would be better to parse the pax extended attributes first
1422 * and then skip any fields in the standard header that were
1423 * defined in the pax header.
1425 err2 = pax_header(a, tar, entry, tar->pax_header.s);
1426 err = err_combine(err, err2);
1427 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1433 * Parse a file header for a Posix "ustar" archive entry. This also
1434 * handles "pax" or "extended ustar" entries.
1437 header_ustar(struct archive_read *a, struct tar *tar,
1438 struct archive_entry *entry, const void *h)
1440 const struct archive_entry_header_ustar *header;
1441 struct archive_string *as;
1442 int err = ARCHIVE_OK, r;
1444 header = (const struct archive_entry_header_ustar *)h;
1446 /* Copy name into an internal buffer to ensure null-termination. */
1447 as = &(tar->entry_pathname);
1448 if (header->prefix[0]) {
1449 archive_strncpy(as, header->prefix, sizeof(header->prefix));
1450 if (as->s[archive_strlen(as) - 1] != '/')
1451 archive_strappend_char(as, '/');
1452 archive_strncat(as, header->name, sizeof(header->name));
1454 archive_strncpy(as, header->name, sizeof(header->name));
1456 if (archive_entry_copy_pathname_l(entry, as->s, archive_strlen(as),
1458 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1459 if (err == ARCHIVE_FATAL)
1463 /* Handle rest of common fields. */
1464 r = header_common(a, tar, entry, h);
1465 if (r == ARCHIVE_FATAL)
1470 /* Handle POSIX ustar fields. */
1471 if (archive_entry_copy_uname_l(entry,
1472 header->uname, sizeof(header->uname), tar->sconv) != 0) {
1473 err = set_conversion_failed_error(a, tar->sconv, "Uname");
1474 if (err == ARCHIVE_FATAL)
1478 if (archive_entry_copy_gname_l(entry,
1479 header->gname, sizeof(header->gname), tar->sconv) != 0) {
1480 err = set_conversion_failed_error(a, tar->sconv, "Gname");
1481 if (err == ARCHIVE_FATAL)
1485 /* Parse out device numbers only for char and block specials. */
1486 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1487 archive_entry_set_rdevmajor(entry, (dev_t)
1488 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
1489 archive_entry_set_rdevminor(entry, (dev_t)
1490 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1493 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1500 * Parse the pax extended attributes record.
1502 * Returns non-zero if there's an error in the data.
1505 pax_header(struct archive_read *a, struct tar *tar,
1506 struct archive_entry *entry, char *attr)
1508 size_t attr_length, l, line_length;
1511 struct archive_string *as;
1512 struct archive_string_conv *sconv;
1515 attr_length = strlen(attr);
1516 tar->pax_hdrcharset_binary = 0;
1517 archive_string_empty(&(tar->entry_gname));
1518 archive_string_empty(&(tar->entry_linkpath));
1519 archive_string_empty(&(tar->entry_pathname));
1520 archive_string_empty(&(tar->entry_pathname_override));
1521 archive_string_empty(&(tar->entry_uname));
1523 while (attr_length > 0) {
1524 /* Parse decimal length field at start of line. */
1527 p = attr; /* Record start of line. */
1534 if (*p < '0' || *p > '9') {
1535 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1536 "Ignoring malformed pax extended attributes");
1537 return (ARCHIVE_WARN);
1540 line_length += *p - '0';
1541 if (line_length > 999999) {
1542 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1543 "Rejecting pax extended attribute > 1MB");
1544 return (ARCHIVE_WARN);
1551 * Parsed length must be no bigger than available data,
1552 * at least 1, and the last character of the line must
1555 if (line_length > attr_length
1557 || attr[line_length - 1] != '\n')
1559 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1560 "Ignoring malformed pax extended attribute");
1561 return (ARCHIVE_WARN);
1564 /* Null-terminate the line. */
1565 attr[line_length - 1] = '\0';
1567 /* Find end of key and null terminate it. */
1571 while (*p && *p != '=')
1574 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1575 "Invalid pax extended attributes");
1576 return (ARCHIVE_WARN);
1580 /* Identify null-terminated 'value' portion. */
1583 /* Identify this attribute and set it in the entry. */
1584 err2 = pax_attribute(a, tar, entry, key, value);
1585 if (err2 == ARCHIVE_FATAL)
1587 err = err_combine(err, err2);
1589 /* Skip to next line */
1590 attr += line_length;
1591 attr_length -= line_length;
1595 * PAX format uses UTF-8 as default charset for its metadata
1596 * unless hdrcharset=BINARY is present in its header.
1597 * We apply the charset specified by the hdrcharset option only
1598 * when the hdrcharset attribute(in PAX header) is BINARY because
1599 * we respect the charset described in PAX header and BINARY also
1600 * means that metadata(filename,uname and gname) character-set
1603 if (tar->pax_hdrcharset_binary)
1604 sconv = tar->opt_sconv;
1606 sconv = archive_string_conversion_from_charset(
1607 &(a->archive), "UTF-8", 1);
1609 return (ARCHIVE_FATAL);
1611 archive_string_conversion_set_opt(sconv,
1612 SCONV_SET_OPT_UTF8_LIBARCHIVE2X);
1615 if (archive_strlen(&(tar->entry_gname)) > 0) {
1616 if (archive_entry_copy_gname_l(entry, tar->entry_gname.s,
1617 archive_strlen(&(tar->entry_gname)), sconv) != 0) {
1618 err = set_conversion_failed_error(a, sconv, "Gname");
1619 if (err == ARCHIVE_FATAL)
1621 /* Use a converted an original name. */
1622 archive_entry_copy_gname(entry, tar->entry_gname.s);
1625 if (archive_strlen(&(tar->entry_linkpath)) > 0) {
1626 if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s,
1627 archive_strlen(&(tar->entry_linkpath)), sconv) != 0) {
1628 err = set_conversion_failed_error(a, sconv, "Linkname");
1629 if (err == ARCHIVE_FATAL)
1631 /* Use a converted an original name. */
1632 archive_entry_copy_link(entry, tar->entry_linkpath.s);
1636 * Some extensions (such as the GNU sparse file extensions)
1637 * deliberately store a synthetic name under the regular 'path'
1638 * attribute and the real file name under a different attribute.
1639 * Since we're supposed to not care about the order, we
1640 * have no choice but to store all of the various filenames
1641 * we find and figure it all out afterwards. This is the
1642 * figuring out part.
1645 if (archive_strlen(&(tar->entry_pathname_override)) > 0)
1646 as = &(tar->entry_pathname_override);
1647 else if (archive_strlen(&(tar->entry_pathname)) > 0)
1648 as = &(tar->entry_pathname);
1650 if (archive_entry_copy_pathname_l(entry, as->s,
1651 archive_strlen(as), sconv) != 0) {
1652 err = set_conversion_failed_error(a, sconv, "Pathname");
1653 if (err == ARCHIVE_FATAL)
1655 /* Use a converted an original name. */
1656 archive_entry_copy_pathname(entry, as->s);
1659 if (archive_strlen(&(tar->entry_uname)) > 0) {
1660 if (archive_entry_copy_uname_l(entry, tar->entry_uname.s,
1661 archive_strlen(&(tar->entry_uname)), sconv) != 0) {
1662 err = set_conversion_failed_error(a, sconv, "Uname");
1663 if (err == ARCHIVE_FATAL)
1665 /* Use a converted an original name. */
1666 archive_entry_copy_uname(entry, tar->entry_uname.s);
1673 pax_attribute_xattr(struct archive_entry *entry,
1674 const char *name, const char *value)
1677 void *value_decoded;
1680 if (strlen(name) < 18 || (memcmp(name, "LIBARCHIVE.xattr.", 17)) != 0)
1685 /* URL-decode name */
1686 name_decoded = url_decode(name);
1687 if (name_decoded == NULL)
1690 /* Base-64 decode value */
1691 value_decoded = base64_decode(value, strlen(value), &value_len);
1692 if (value_decoded == NULL) {
1697 archive_entry_xattr_add_entry(entry, name_decoded,
1698 value_decoded, value_len);
1701 free(value_decoded);
1706 * Parse a single key=value attribute. key/value pointers are
1707 * assumed to point into reasonably long-lived storage.
1709 * Note that POSIX reserves all-lowercase keywords. Vendor-specific
1710 * extensions should always have keywords of the form "VENDOR.attribute"
1711 * In particular, it's quite feasible to support many different
1712 * vendor extensions here. I'm using "LIBARCHIVE" for extensions
1713 * unique to this library.
1715 * Investigate other vendor-specific extensions and see if
1716 * any of them look useful.
1719 pax_attribute(struct archive_read *a, struct tar *tar,
1720 struct archive_entry *entry, const char *key, const char *value)
1724 int err = ARCHIVE_OK, r;
1728 value = ""; /* Disable compiler warning; do not pass
1729 * NULL pointer to strlen(). */
1733 /* GNU "0.0" sparse pax format. */
1734 if (strcmp(key, "GNU.sparse.numblocks") == 0) {
1735 tar->sparse_offset = -1;
1736 tar->sparse_numbytes = -1;
1737 tar->sparse_gnu_major = 0;
1738 tar->sparse_gnu_minor = 0;
1740 if (strcmp(key, "GNU.sparse.offset") == 0) {
1741 tar->sparse_offset = tar_atol10(value, strlen(value));
1742 if (tar->sparse_numbytes != -1) {
1743 if (gnu_add_sparse_entry(a, tar,
1744 tar->sparse_offset, tar->sparse_numbytes)
1746 return (ARCHIVE_FATAL);
1747 tar->sparse_offset = -1;
1748 tar->sparse_numbytes = -1;
1751 if (strcmp(key, "GNU.sparse.numbytes") == 0) {
1752 tar->sparse_numbytes = tar_atol10(value, strlen(value));
1753 if (tar->sparse_numbytes != -1) {
1754 if (gnu_add_sparse_entry(a, tar,
1755 tar->sparse_offset, tar->sparse_numbytes)
1757 return (ARCHIVE_FATAL);
1758 tar->sparse_offset = -1;
1759 tar->sparse_numbytes = -1;
1762 if (strcmp(key, "GNU.sparse.size") == 0) {
1763 tar->realsize = tar_atol10(value, strlen(value));
1764 archive_entry_set_size(entry, tar->realsize);
1767 /* GNU "0.1" sparse pax format. */
1768 if (strcmp(key, "GNU.sparse.map") == 0) {
1769 tar->sparse_gnu_major = 0;
1770 tar->sparse_gnu_minor = 1;
1771 if (gnu_sparse_01_parse(a, tar, value) != ARCHIVE_OK)
1772 return (ARCHIVE_WARN);
1775 /* GNU "1.0" sparse pax format */
1776 if (strcmp(key, "GNU.sparse.major") == 0) {
1777 tar->sparse_gnu_major = (int)tar_atol10(value, strlen(value));
1778 tar->sparse_gnu_pending = 1;
1780 if (strcmp(key, "GNU.sparse.minor") == 0) {
1781 tar->sparse_gnu_minor = (int)tar_atol10(value, strlen(value));
1782 tar->sparse_gnu_pending = 1;
1784 if (strcmp(key, "GNU.sparse.name") == 0) {
1786 * The real filename; when storing sparse
1787 * files, GNU tar puts a synthesized name into
1788 * the regular 'path' attribute in an attempt
1789 * to limit confusion. ;-)
1791 archive_strcpy(&(tar->entry_pathname_override), value);
1793 if (strcmp(key, "GNU.sparse.realsize") == 0) {
1794 tar->realsize = tar_atol10(value, strlen(value));
1795 archive_entry_set_size(entry, tar->realsize);
1799 /* Our extensions */
1800 /* TODO: Handle arbitrary extended attributes... */
1802 if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0)
1803 archive_entry_set_xxxxxx(entry, value);
1805 if (strcmp(key, "LIBARCHIVE.creationtime") == 0) {
1806 pax_time(value, &s, &n);
1807 archive_entry_set_birthtime(entry, s, n);
1809 if (memcmp(key, "LIBARCHIVE.xattr.", 17) == 0)
1810 pax_attribute_xattr(entry, key, value);
1813 /* We support some keys used by the "star" archiver */
1814 if (strcmp(key, "SCHILY.acl.access") == 0) {
1815 if (tar->sconv_acl == NULL) {
1817 archive_string_conversion_from_charset(
1818 &(a->archive), "UTF-8", 1);
1819 if (tar->sconv_acl == NULL)
1820 return (ARCHIVE_FATAL);
1823 r = archive_acl_parse_l(archive_entry_acl(entry),
1824 value, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
1826 if (r != ARCHIVE_OK) {
1828 if (err == ARCHIVE_FATAL) {
1829 archive_set_error(&a->archive, ENOMEM,
1830 "Can't allocate memory for "
1831 "SCHILY.acl.access");
1834 archive_set_error(&a->archive,
1836 "Parse error: SCHILY.acl.access");
1838 } else if (strcmp(key, "SCHILY.acl.default") == 0) {
1839 if (tar->sconv_acl == NULL) {
1841 archive_string_conversion_from_charset(
1842 &(a->archive), "UTF-8", 1);
1843 if (tar->sconv_acl == NULL)
1844 return (ARCHIVE_FATAL);
1847 r = archive_acl_parse_l(archive_entry_acl(entry),
1848 value, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT,
1850 if (r != ARCHIVE_OK) {
1852 if (err == ARCHIVE_FATAL) {
1853 archive_set_error(&a->archive, ENOMEM,
1854 "Can't allocate memory for "
1855 "SCHILY.acl.default");
1858 archive_set_error(&a->archive,
1860 "Parse error: SCHILY.acl.default");
1862 } else if (strcmp(key, "SCHILY.devmajor") == 0) {
1863 archive_entry_set_rdevmajor(entry,
1864 (dev_t)tar_atol10(value, strlen(value)));
1865 } else if (strcmp(key, "SCHILY.devminor") == 0) {
1866 archive_entry_set_rdevminor(entry,
1867 (dev_t)tar_atol10(value, strlen(value)));
1868 } else if (strcmp(key, "SCHILY.fflags") == 0) {
1869 archive_entry_copy_fflags_text(entry, value);
1870 } else if (strcmp(key, "SCHILY.dev") == 0) {
1871 archive_entry_set_dev(entry,
1872 (dev_t)tar_atol10(value, strlen(value)));
1873 } else if (strcmp(key, "SCHILY.ino") == 0) {
1874 archive_entry_set_ino(entry,
1875 tar_atol10(value, strlen(value)));
1876 } else if (strcmp(key, "SCHILY.nlink") == 0) {
1877 archive_entry_set_nlink(entry, (unsigned)
1878 tar_atol10(value, strlen(value)));
1879 } else if (strcmp(key, "SCHILY.realsize") == 0) {
1880 tar->realsize = tar_atol10(value, strlen(value));
1881 archive_entry_set_size(entry, tar->realsize);
1882 } else if (strcmp(key, "SUN.holesdata") == 0) {
1883 /* A Solaris extension for sparse. */
1884 r = solaris_sparse_parse(a, tar, entry, value);
1886 if (r == ARCHIVE_FATAL)
1889 archive_set_error(&a->archive,
1891 "Parse error: SUN.holesdata");
1896 if (strcmp(key, "atime") == 0) {
1897 pax_time(value, &s, &n);
1898 archive_entry_set_atime(entry, s, n);
1902 if (strcmp(key, "ctime") == 0) {
1903 pax_time(value, &s, &n);
1904 archive_entry_set_ctime(entry, s, n);
1905 } else if (strcmp(key, "charset") == 0) {
1906 /* TODO: Publish charset information in entry. */
1907 } else if (strcmp(key, "comment") == 0) {
1908 /* TODO: Publish comment in entry. */
1912 if (strcmp(key, "gid") == 0) {
1913 archive_entry_set_gid(entry,
1914 tar_atol10(value, strlen(value)));
1915 } else if (strcmp(key, "gname") == 0) {
1916 archive_strcpy(&(tar->entry_gname), value);
1920 if (strcmp(key, "hdrcharset") == 0) {
1921 if (strcmp(value, "BINARY") == 0)
1923 tar->pax_hdrcharset_binary = 1;
1924 else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0)
1925 tar->pax_hdrcharset_binary = 0;
1929 /* pax interchange doesn't distinguish hardlink vs. symlink. */
1930 if (strcmp(key, "linkpath") == 0) {
1931 archive_strcpy(&(tar->entry_linkpath), value);
1935 if (strcmp(key, "mtime") == 0) {
1936 pax_time(value, &s, &n);
1937 archive_entry_set_mtime(entry, s, n);
1941 if (strcmp(key, "path") == 0) {
1942 archive_strcpy(&(tar->entry_pathname), value);
1946 /* POSIX has reserved 'realtime.*' */
1949 /* POSIX has reserved 'security.*' */
1950 /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */
1951 if (strcmp(key, "size") == 0) {
1952 /* "size" is the size of the data in the entry. */
1953 tar->entry_bytes_remaining
1954 = tar_atol10(value, strlen(value));
1956 * But, "size" is not necessarily the size of
1957 * the file on disk; if this is a sparse file,
1958 * the disk size may have already been set from
1959 * GNU.sparse.realsize or GNU.sparse.size or
1960 * an old GNU header field or SCHILY.realsize
1963 if (tar->realsize < 0) {
1964 archive_entry_set_size(entry,
1965 tar->entry_bytes_remaining);
1967 = tar->entry_bytes_remaining;
1972 if (strcmp(key, "uid") == 0) {
1973 archive_entry_set_uid(entry,
1974 tar_atol10(value, strlen(value)));
1975 } else if (strcmp(key, "uname") == 0) {
1976 archive_strcpy(&(tar->entry_uname), value);
1986 * parse a decimal time value, which may include a fractional portion
1989 pax_time(const char *p, int64_t *ps, long *pn)
1995 int64_t limit, last_digit_limit;
1997 limit = INT64_MAX / 10;
1998 last_digit_limit = INT64_MAX % 10;
2006 while (*p >= '0' && *p <= '9') {
2009 (s == limit && digit > last_digit_limit)) {
2013 s = (s * 10) + digit;
2019 /* Calculate nanoseconds. */
2028 if (*p >= '0' && *p <= '9')
2029 *pn += (*p - '0') * l;
2036 * Parse GNU tar header
2039 header_gnutar(struct archive_read *a, struct tar *tar,
2040 struct archive_entry *entry, const void *h, size_t *unconsumed)
2042 const struct archive_entry_header_gnutar *header;
2044 int err = ARCHIVE_OK;
2047 * GNU header is like POSIX ustar, except 'prefix' is
2048 * replaced with some other fields. This also means the
2049 * filename is stored as in old-style archives.
2052 /* Grab fields common to all tar variants. */
2053 err = header_common(a, tar, entry, h);
2054 if (err == ARCHIVE_FATAL)
2057 /* Copy filename over (to ensure null termination). */
2058 header = (const struct archive_entry_header_gnutar *)h;
2059 if (archive_entry_copy_pathname_l(entry,
2060 header->name, sizeof(header->name), tar->sconv) != 0) {
2061 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
2062 if (err == ARCHIVE_FATAL)
2066 /* Fields common to ustar and GNU */
2067 /* XXX Can the following be factored out since it's common
2068 * to ustar and gnu tar? Is it okay to move it down into
2069 * header_common, perhaps? */
2070 if (archive_entry_copy_uname_l(entry,
2071 header->uname, sizeof(header->uname), tar->sconv) != 0) {
2072 err = set_conversion_failed_error(a, tar->sconv, "Uname");
2073 if (err == ARCHIVE_FATAL)
2077 if (archive_entry_copy_gname_l(entry,
2078 header->gname, sizeof(header->gname), tar->sconv) != 0) {
2079 err = set_conversion_failed_error(a, tar->sconv, "Gname");
2080 if (err == ARCHIVE_FATAL)
2084 /* Parse out device numbers only for char and block specials */
2085 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
2086 archive_entry_set_rdevmajor(entry, (dev_t)
2087 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
2088 archive_entry_set_rdevminor(entry, (dev_t)
2089 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
2091 archive_entry_set_rdev(entry, 0);
2093 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
2095 /* Grab GNU-specific fields. */
2096 t = tar_atol(header->atime, sizeof(header->atime));
2098 archive_entry_set_atime(entry, t, 0);
2099 t = tar_atol(header->ctime, sizeof(header->ctime));
2101 archive_entry_set_ctime(entry, t, 0);
2103 if (header->realsize[0] != 0) {
2105 = tar_atol(header->realsize, sizeof(header->realsize));
2106 archive_entry_set_size(entry, tar->realsize);
2109 if (header->sparse[0].offset[0] != 0) {
2110 if (gnu_sparse_old_read(a, tar, header, unconsumed)
2112 return (ARCHIVE_FATAL);
2114 if (header->isextended[0] != 0) {
2123 gnu_add_sparse_entry(struct archive_read *a, struct tar *tar,
2124 int64_t offset, int64_t remaining)
2126 struct sparse_block *p;
2128 p = (struct sparse_block *)malloc(sizeof(*p));
2130 archive_set_error(&a->archive, ENOMEM, "Out of memory");
2131 return (ARCHIVE_FATAL);
2133 memset(p, 0, sizeof(*p));
2134 if (tar->sparse_last != NULL)
2135 tar->sparse_last->next = p;
2137 tar->sparse_list = p;
2138 tar->sparse_last = p;
2139 if (remaining < 0 || offset < 0) {
2140 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Malformed sparse map data");
2141 return (ARCHIVE_FATAL);
2144 p->remaining = remaining;
2145 return (ARCHIVE_OK);
2149 gnu_clear_sparse_list(struct tar *tar)
2151 struct sparse_block *p;
2153 while (tar->sparse_list != NULL) {
2154 p = tar->sparse_list;
2155 tar->sparse_list = p->next;
2158 tar->sparse_last = NULL;
2162 * GNU tar old-format sparse data.
2164 * GNU old-format sparse data is stored in a fixed-field
2165 * format. Offset/size values are 11-byte octal fields (same
2166 * format as 'size' field in ustart header). These are
2167 * stored in the header, allocating subsequent header blocks
2168 * as needed. Extending the header in this way is a pretty
2169 * severe POSIX violation; this design has earned GNU tar a
2174 gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
2175 const struct archive_entry_header_gnutar *header, size_t *unconsumed)
2180 struct gnu_sparse sparse[21];
2184 const struct extended *ext;
2186 if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK)
2187 return (ARCHIVE_FATAL);
2188 if (header->isextended[0] == 0)
2189 return (ARCHIVE_OK);
2192 tar_flush_unconsumed(a, unconsumed);
2193 data = __archive_read_ahead(a, 512, &bytes_read);
2195 return (ARCHIVE_FATAL);
2196 if (bytes_read < 512) {
2197 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2198 "Truncated tar archive "
2199 "detected while reading sparse file data");
2200 return (ARCHIVE_FATAL);
2203 ext = (const struct extended *)data;
2204 if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK)
2205 return (ARCHIVE_FATAL);
2206 } while (ext->isextended[0] != 0);
2207 if (tar->sparse_list != NULL)
2208 tar->entry_offset = tar->sparse_list->offset;
2209 return (ARCHIVE_OK);
2213 gnu_sparse_old_parse(struct archive_read *a, struct tar *tar,
2214 const struct gnu_sparse *sparse, int length)
2216 while (length > 0 && sparse->offset[0] != 0) {
2217 if (gnu_add_sparse_entry(a, tar,
2218 tar_atol(sparse->offset, sizeof(sparse->offset)),
2219 tar_atol(sparse->numbytes, sizeof(sparse->numbytes)))
2221 return (ARCHIVE_FATAL);
2225 return (ARCHIVE_OK);
2229 * GNU tar sparse format 0.0
2231 * Beginning with GNU tar 1.15, sparse files are stored using
2232 * information in the pax extended header. The GNU tar maintainers
2233 * have gone through a number of variations in the process of working
2234 * out this scheme; fortunately, they're all numbered.
2236 * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
2237 * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
2238 * store offset/size for each block. The repeated instances of these
2239 * latter fields violate the pax specification (which frowns on
2240 * duplicate keys), so this format was quickly replaced.
2244 * GNU tar sparse format 0.1
2246 * This version replaced the offset/numbytes attributes with
2247 * a single "map" attribute that stored a list of integers. This
2248 * format had two problems: First, the "map" attribute could be very
2249 * long, which caused problems for some implementations. More
2250 * importantly, the sparse data was lost when extracted by archivers
2251 * that didn't recognize this extension.
2255 gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p)
2258 int64_t offset = -1, size = -1;
2262 while (*e != '\0' && *e != ',') {
2263 if (*e < '0' || *e > '9')
2264 return (ARCHIVE_WARN);
2268 offset = tar_atol10(p, e - p);
2270 return (ARCHIVE_WARN);
2272 size = tar_atol10(p, e - p);
2274 return (ARCHIVE_WARN);
2275 if (gnu_add_sparse_entry(a, tar, offset, size)
2277 return (ARCHIVE_FATAL);
2281 return (ARCHIVE_OK);
2287 * GNU tar sparse format 1.0
2289 * The idea: The offset/size data is stored as a series of base-10
2290 * ASCII numbers prepended to the file data, so that dearchivers that
2291 * don't support this format will extract the block map along with the
2292 * data and a separate post-process can restore the sparseness.
2294 * Unfortunately, GNU tar 1.16 had a bug that added unnecessary
2295 * padding to the body of the file when using this format. GNU tar
2296 * 1.17 corrected this bug without bumping the version number, so
2297 * it's not possible to support both variants. This code supports
2298 * the later variant at the expense of not supporting the former.
2300 * This variant also replaced GNU.sparse.size with GNU.sparse.realsize
2301 * and introduced the GNU.sparse.major/GNU.sparse.minor attributes.
2305 * Read the next line from the input, and parse it as a decimal
2306 * integer followed by '\n'. Returns positive integer value or
2307 * negative on error.
2310 gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
2311 int64_t *remaining, size_t *unconsumed)
2313 int64_t l, limit, last_digit_limit;
2319 limit = INT64_MAX / base;
2320 last_digit_limit = INT64_MAX % base;
2323 * Skip any lines starting with '#'; GNU tar specs
2324 * don't require this, but they should.
2327 bytes_read = readline(a, tar, &p,
2328 (ssize_t)tar_min(*remaining, 100), unconsumed);
2329 if (bytes_read <= 0)
2330 return (ARCHIVE_FATAL);
2331 *remaining -= bytes_read;
2332 } while (p[0] == '#');
2335 while (bytes_read > 0) {
2338 if (*p < '0' || *p >= '0' + base)
2339 return (ARCHIVE_WARN);
2341 if (l > limit || (l == limit && digit > last_digit_limit))
2342 l = INT64_MAX; /* Truncate on overflow. */
2344 l = (l * base) + digit;
2348 /* TODO: Error message. */
2349 return (ARCHIVE_WARN);
2353 * Returns length (in bytes) of the sparse data description
2357 gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed)
2361 int64_t offset, size, to_skip, remaining;
2363 /* Clear out the existing sparse list. */
2364 gnu_clear_sparse_list(tar);
2366 remaining = tar->entry_bytes_remaining;
2368 /* Parse entries. */
2369 entries = (int)gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2371 return (ARCHIVE_FATAL);
2372 /* Parse the individual entries. */
2373 while (entries-- > 0) {
2374 /* Parse offset/size */
2375 offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2377 return (ARCHIVE_FATAL);
2378 size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2380 return (ARCHIVE_FATAL);
2381 /* Add a new sparse entry. */
2382 if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK)
2383 return (ARCHIVE_FATAL);
2385 /* Skip rest of block... */
2386 tar_flush_unconsumed(a, unconsumed);
2387 bytes_read = (ssize_t)(tar->entry_bytes_remaining - remaining);
2388 to_skip = 0x1ff & -bytes_read;
2389 if (to_skip != __archive_read_consume(a, to_skip))
2390 return (ARCHIVE_FATAL);
2391 return ((ssize_t)(bytes_read + to_skip));
2395 * Solaris pax extension for a sparse file. This is recorded with the
2396 * data and hole pairs. The way recording sparse information by Solaris'
2397 * pax simply indicates where data and sparse are, so the stored contents
2398 * consist of both data and hole.
2401 solaris_sparse_parse(struct archive_read *a, struct tar *tar,
2402 struct archive_entry *entry, const char *p)
2408 (void)entry; /* UNUSED */
2414 return (ARCHIVE_WARN);
2417 while (*e != '\0' && *e != ' ') {
2418 if (*e < '0' || *e > '9')
2419 return (ARCHIVE_WARN);
2423 end = tar_atol10(p, e - p);
2425 return (ARCHIVE_WARN);
2427 if (gnu_add_sparse_entry(a, tar, start,
2428 end - start) != ARCHIVE_OK)
2429 return (ARCHIVE_FATAL);
2430 tar->sparse_last->hole = hole;
2433 return (ARCHIVE_OK);
2440 * Convert text->integer.
2442 * Traditional tar formats (including POSIX) specify base-8 for
2443 * all of the standard numeric fields. This is a significant limitation
2445 * = file size is limited to 8GB
2446 * = rdevmajor and rdevminor are limited to 21 bits
2447 * = uid/gid are limited to 21 bits
2449 * There are two workarounds for this:
2450 * = pax extended headers, which use variable-length string fields
2451 * = GNU tar and STAR both allow either base-8 or base-256 in
2452 * most fields. The high bit is set to indicate base-256.
2454 * On read, this implementation supports both extensions.
2457 tar_atol(const char *p, size_t char_cnt)
2460 * Technically, GNU tar considers a field to be in base-256
2461 * only if the first byte is 0xff or 0x80.
2464 return (tar_atol256(p, char_cnt));
2465 return (tar_atol8(p, char_cnt));
2469 * Note that this implementation does not (and should not!) obey
2470 * locale settings; you cannot simply substitute strtol here, since
2471 * it does obey locale.
2474 tar_atol_base_n(const char *p, size_t char_cnt, int base)
2476 int64_t l, maxval, limit, last_digit_limit;
2480 limit = INT64_MAX / base;
2481 last_digit_limit = INT64_MAX % base;
2483 /* the pointer will not be dereferenced if char_cnt is zero
2484 * due to the way the && operator is evaulated.
2486 while (char_cnt != 0 && (*p == ' ' || *p == '\t')) {
2492 if (char_cnt != 0 && *p == '-') {
2498 limit = -(INT64_MIN / base);
2499 last_digit_limit = INT64_MIN % base;
2503 if (char_cnt != 0) {
2505 while (digit >= 0 && digit < base && char_cnt != 0) {
2506 if (l>limit || (l == limit && digit > last_digit_limit)) {
2507 return maxval; /* Truncate on overflow. */
2509 l = (l * base) + digit;
2514 return (sign < 0) ? -l : l;
2518 tar_atol8(const char *p, size_t char_cnt)
2520 return tar_atol_base_n(p, char_cnt, 8);
2524 tar_atol10(const char *p, size_t char_cnt)
2526 return tar_atol_base_n(p, char_cnt, 10);
2530 * Parse a base-256 integer. This is just a variable-length
2531 * twos-complement signed binary value in big-endian order, except
2532 * that the high-order bit is ignored. The values here can be up to
2533 * 12 bytes, so we need to be careful about overflowing 64-bit
2534 * (8-byte) integers.
2536 * This code unashamedly assumes that the local machine uses 8-bit
2537 * bytes and twos-complement arithmetic.
2540 tar_atol256(const char *_p, size_t char_cnt)
2543 const unsigned char *p = (const unsigned char *)_p;
2544 unsigned char c, neg;
2546 /* Extend 7-bit 2s-comp to 8-bit 2s-comp, decide sign. */
2551 l = ~ARCHIVE_LITERAL_ULL(0);
2558 /* If more than 8 bytes, check that we can ignore
2559 * high-order bits without overflow. */
2560 while (char_cnt > sizeof(int64_t)) {
2563 return neg ? INT64_MIN : INT64_MAX;
2567 /* c is first byte that fits; if sign mismatch, return overflow */
2568 if ((c ^ neg) & 0x80) {
2569 return neg ? INT64_MIN : INT64_MAX;
2572 /* Accumulate remaining bytes. */
2573 while (--char_cnt > 0) {
2578 /* Return signed twos-complement value. */
2579 return (int64_t)(l);
2583 * Returns length of line (including trailing newline)
2584 * or negative on error. 'start' argument is updated to
2585 * point to first character of line. This avoids copying
2589 readline(struct archive_read *a, struct tar *tar, const char **start,
2590 ssize_t limit, size_t *unconsumed)
2593 ssize_t total_size = 0;
2598 tar_flush_unconsumed(a, unconsumed);
2600 t = __archive_read_ahead(a, 1, &bytes_read);
2601 if (bytes_read <= 0)
2602 return (ARCHIVE_FATAL);
2603 s = t; /* Start of line? */
2604 p = memchr(t, '\n', bytes_read);
2605 /* If we found '\n' in the read buffer, return pointer to that. */
2607 bytes_read = 1 + ((const char *)p) - s;
2608 if (bytes_read > limit) {
2609 archive_set_error(&a->archive,
2610 ARCHIVE_ERRNO_FILE_FORMAT,
2612 return (ARCHIVE_FATAL);
2614 *unconsumed = bytes_read;
2616 return (bytes_read);
2618 *unconsumed = bytes_read;
2619 /* Otherwise, we need to accumulate in a line buffer. */
2621 if (total_size + bytes_read > limit) {
2622 archive_set_error(&a->archive,
2623 ARCHIVE_ERRNO_FILE_FORMAT,
2625 return (ARCHIVE_FATAL);
2627 if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) {
2628 archive_set_error(&a->archive, ENOMEM,
2629 "Can't allocate working buffer");
2630 return (ARCHIVE_FATAL);
2632 memcpy(tar->line.s + total_size, t, bytes_read);
2633 tar_flush_unconsumed(a, unconsumed);
2634 total_size += bytes_read;
2635 /* If we found '\n', clean up and return. */
2637 *start = tar->line.s;
2638 return (total_size);
2640 /* Read some more. */
2641 t = __archive_read_ahead(a, 1, &bytes_read);
2642 if (bytes_read <= 0)
2643 return (ARCHIVE_FATAL);
2644 s = t; /* Start of line? */
2645 p = memchr(t, '\n', bytes_read);
2646 /* If we found '\n', trim the read. */
2648 bytes_read = 1 + ((const char *)p) - s;
2650 *unconsumed = bytes_read;
2655 * base64_decode - Base64 decode
2657 * This accepts most variations of base-64 encoding, including:
2658 * * with or without line breaks
2659 * * with or without the final group padded with '=' or '_' characters
2660 * (The most economical Base-64 variant does not pad the last group and
2661 * omits line breaks; RFC1341 used for MIME requires both.)
2664 base64_decode(const char *s, size_t len, size_t *out_len)
2666 static const unsigned char digits[64] = {
2667 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
2668 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
2669 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
2670 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
2671 '4','5','6','7','8','9','+','/' };
2672 static unsigned char decode_table[128];
2674 const unsigned char *src = (const unsigned char *)s;
2676 /* If the decode table is not yet initialized, prepare it. */
2677 if (decode_table[digits[1]] != 1) {
2679 memset(decode_table, 0xff, sizeof(decode_table));
2680 for (i = 0; i < sizeof(digits); i++)
2681 decode_table[digits[i]] = i;
2684 /* Allocate enough space to hold the entire output. */
2685 /* Note that we may not use all of this... */
2686 out = (char *)malloc(len - len / 4 + 1);
2694 /* Collect the next group of (up to) four characters. */
2697 while (group_size < 4 && len > 0) {
2698 /* '=' or '_' padding indicates final group. */
2699 if (*src == '=' || *src == '_') {
2703 /* Skip illegal characters (including line breaks) */
2704 if (*src > 127 || *src < 32
2705 || decode_table[*src] == 0xff) {
2711 v |= decode_table[*src++];
2715 /* Align a short group properly. */
2716 v <<= 6 * (4 - group_size);
2717 /* Unpack the group we just collected. */
2718 switch (group_size) {
2719 case 4: d[2] = v & 0xff;
2721 case 3: d[1] = (v >> 8) & 0xff;
2723 case 2: d[0] = (v >> 16) & 0xff;
2725 case 1: /* this is invalid! */
2728 d += group_size * 3 / 4;
2736 url_decode(const char *in)
2741 out = (char *)malloc(strlen(in) + 1);
2744 for (s = in, d = out; *s != '\0'; ) {
2745 if (s[0] == '%' && s[1] != '\0' && s[2] != '\0') {
2746 /* Try to convert % escape */
2747 int digit1 = tohex(s[1]);
2748 int digit2 = tohex(s[2]);
2749 if (digit1 >= 0 && digit2 >= 0) {
2750 /* Looks good, consume three chars */
2752 /* Convert output */
2753 *d++ = ((digit1 << 4) | digit2);
2756 /* Else fall through and treat '%' as normal char */
2767 if (c >= '0' && c <= '9')
2769 else if (c >= 'A' && c <= 'F')
2770 return (c - 'A' + 10);
2771 else if (c >= 'a' && c <= 'f')
2772 return (c - 'a' + 10);